Skip to content

Commit

Permalink
add error
Browse files Browse the repository at this point in the history
  • Loading branch information
raulb committed Jul 1, 2024
1 parent da3a2e4 commit ef0d775
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
20 changes: 20 additions & 0 deletions schema/errors.go
Original file line number Diff line number Diff line change
@@ -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")
6 changes: 4 additions & 2 deletions schema/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
57 changes: 42 additions & 15 deletions schema/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})
}
}

0 comments on commit ef0d775

Please sign in to comment.