From ef0d77529f8efd3a63443afffbd64572f8c544ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Barroso?= Date: Mon, 1 Jul 2024 18:03:57 +0200 Subject: [PATCH] add error --- schema/errors.go | 20 ++++++++++++++++ schema/proto.go | 6 +++-- schema/proto_test.go | 57 ++++++++++++++++++++++++++++++++------------ 3 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 schema/errors.go diff --git a/schema/errors.go b/schema/errors.go new file mode 100644 index 0000000..bab1b03 --- /dev/null +++ b/schema/errors.go @@ -0,0 +1,20 @@ +// Copyright © 2024 Meroxa, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package schema + +import "errors" + +// errInvalidProtoIsNil is returned when trying to convert a proto schema to a schema object. +var errInvalidProtoIsNil = errors.New("invalid proto: nil") diff --git a/schema/proto.go b/schema/proto.go index 875a87e..0cc3bd6 100644 --- a/schema/proto.go +++ b/schema/proto.go @@ -14,7 +14,9 @@ package schema -import schemav1 "github.com/conduitio/conduit-commons/proto/schema/v1" +import ( + schemav1 "github.com/conduitio/conduit-commons/proto/schema/v1" +) func _() { // An "invalid array index" compiler error signifies that the constant values have changed. @@ -49,7 +51,7 @@ func (s *Schema) FromProto(proto *schemav1.Schema) error { // populated. func (s *Schema) ToProto(proto *schemav1.Schema) error { if proto == nil { - return nil + return errInvalidProtoIsNil } proto.Subject = s.Subject diff --git a/schema/proto_test.go b/schema/proto_test.go index 78e9055..81192bb 100644 --- a/schema/proto_test.go +++ b/schema/proto_test.go @@ -47,22 +47,49 @@ func TestSchema_FromProto(t *testing.T) { func TestSchema_ToProto(t *testing.T) { is := is.New(t) - s1 := Schema{ - Subject: "subject", - Version: 1, - Type: TypeAvro, - Bytes: []byte("bytes"), + testCases := []struct { + name string + in *schemav1.Schema + want *schemav1.Schema + wantErr error + }{ + { + name: "when proto object is nil", + in: nil, + want: nil, + wantErr: errInvalidProtoIsNil, + }, + { + name: "when proto object is not nil", + in: &schemav1.Schema{}, + want: &schemav1.Schema{ + Subject: "subject", + Version: 1, + Type: schemav1.Schema_TYPE_AVRO, + Bytes: []byte("bytes"), + }, + }, } - want := &schemav1.Schema{ - Subject: s1.Subject, - Version: 1, - Type: schemav1.Schema_TYPE_AVRO, - Bytes: s1.Bytes, - } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + is := is.New(t) - var got schemav1.Schema - err := s1.ToProto(&got) - is.NoErr(err) - is.Equal(&got, want) + s1 := Schema{ + Subject: "subject", + Version: 1, + Type: TypeAvro, + Bytes: []byte("bytes"), + } + + err := s1.ToProto(tc.in) + + if tc.wantErr == nil { + is.NoErr(err) + is.Equal(tc.in, tc.want) + } else { + is.Equal(err.Error(), tc.wantErr.Error()) + } + }) + } }