@@ -18,11 +18,13 @@ package avrov2
18
18
19
19
import (
20
20
"errors"
21
- "github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/rules/cel"
22
21
"reflect"
23
22
"testing"
24
23
"time"
25
24
25
+ "github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/rules/cel"
26
+ "github.com/hamba/avro/v2"
27
+
26
28
_ "github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/rules/cel"
27
29
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/rules/encryption"
28
30
_ "github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/rules/encryption/awskms"
@@ -286,7 +288,62 @@ const (
286
288
"confluent:tags": [ "PII" ]
287
289
}
288
290
]
289
- }
291
+ }
292
+ `
293
+ demoWithSchemaFuncSchema = `
294
+ {
295
+ "name": "DemoWithSchemaFunc",
296
+ "type": "record",
297
+ "fields": [
298
+ {
299
+ "name": "IntField",
300
+ "type": "int"
301
+ },
302
+ {
303
+ "name": "BoolField",
304
+ "type": "boolean"
305
+ },
306
+ {
307
+ "name": "ArrayField",
308
+ "type": {
309
+ "type": "array",
310
+ "items": "string"
311
+ }
312
+ },
313
+ {
314
+ "name": "MapField",
315
+ "type": {
316
+ "type": "map",
317
+ "values": "string"
318
+ }
319
+ },
320
+ {
321
+ "name": "StringField",
322
+ "type": ["null", "string"]
323
+ },
324
+ {
325
+ "name": "EnumField",
326
+ "type": {
327
+ "name": "GreetingsEnum",
328
+ "type": "enum",
329
+ "symbols": ["hey", "bye"]
330
+ }
331
+ },
332
+ {
333
+ "name": "RecordField",
334
+ "type": {
335
+ "name": "GreetingsObj",
336
+ "type": "record",
337
+ "fields": [
338
+ {
339
+ "name": "Hey",
340
+ "type": "string"
341
+ }
342
+ ]
343
+ }
344
+ }
345
+ ]
346
+ }
290
347
`
291
348
)
292
349
@@ -302,6 +359,8 @@ func testMessageFactory(subject string, name string) (interface{}, error) {
302
359
return & DemoSchemaSingleTag {}, nil
303
360
case "DemoSchemaWithUnion" :
304
361
return & DemoSchemaWithUnion {}, nil
362
+ case "DemoWithSchemaFunc" :
363
+ return & DemoWithSchemaFunc {}, nil
305
364
case "ComplexSchema" :
306
365
return & ComplexSchema {}, nil
307
366
case "SchemaEvolution" :
@@ -804,6 +863,46 @@ func TestAvroSchemaEvolution(t *testing.T) {
804
863
serde .MaybeFail ("deserialization" , err , serde .Expect (msg , & obj2 ))
805
864
}
806
865
866
+ func TestAvroSerdeWithEncodingSchemaFunc (t * testing.T ) {
867
+ serde .MaybeFail = serde .InitFailFunc (t )
868
+ var err error
869
+ conf := schemaregistry .NewConfig ("mock://" )
870
+
871
+ client , err := schemaregistry .NewClient (conf )
872
+ serde .MaybeFail ("Schema Registry configuration" , err )
873
+
874
+ ser , err := NewSerializer (client , serde .ValueSerde , NewSerializerConfig ())
875
+ serde .MaybeFail ("Serializer configuration" , err )
876
+
877
+ obj := DemoWithSchemaFunc {
878
+ IntField : 123 ,
879
+ StringField : nil ,
880
+ BoolField : true ,
881
+ ArrayField : []string {"hello" , "world" },
882
+ MapField : map [string ]string {
883
+ "hello" : "world" ,
884
+ },
885
+ EnumField : "hey" ,
886
+ RecordField : struct {
887
+ Hey string `json:"Hey"`
888
+ }{Hey : "bye" },
889
+ }
890
+ bytes , err := ser .Serialize ("topic1" , & obj )
891
+ serde .MaybeFail ("serialization" , err )
892
+
893
+ deser , err := NewDeserializer (client , serde .ValueSerde , NewDeserializerConfig ())
894
+ serde .MaybeFail ("Deserializer configuration" , err )
895
+ deser .Client = ser .Client
896
+ deser .MessageFactory = testMessageFactory
897
+
898
+ var newobj DemoWithSchemaFunc
899
+ err = deser .DeserializeInto ("topic1" , bytes , & newobj )
900
+ serde .MaybeFail ("deserialization into" , err , serde .Expect (newobj , obj ))
901
+
902
+ msg , err := deser .Deserialize ("topic1" , bytes )
903
+ serde .MaybeFail ("deserialization" , err , serde .Expect (msg , & obj ))
904
+ }
905
+
807
906
func TestAvroSerdeWithCELCondition (t * testing.T ) {
808
907
serde .MaybeFail = serde .InitFailFunc (t )
809
908
var err error
@@ -2688,3 +2787,19 @@ type SchemaEvolution1 struct {
2688
2787
type SchemaEvolution2 struct {
2689
2788
NewOptionalField string `json:"NewOptionalField"`
2690
2789
}
2790
+
2791
+ type DemoWithSchemaFunc struct {
2792
+ IntField int32 `json:"IntField"`
2793
+ BoolField bool `json:"BoolField"`
2794
+ StringField * string `json:"StringField"`
2795
+ ArrayField []string `json:"ArrayField"`
2796
+ MapField map [string ]string `json:"MapField"`
2797
+ EnumField string `json:"EnumField"`
2798
+ RecordField struct {
2799
+ Hey string `json:"Hey"`
2800
+ } `json:"RecordField"`
2801
+ }
2802
+
2803
+ func (d * DemoWithSchemaFunc ) Schema () avro.Schema {
2804
+ return avro .MustParse (demoWithSchemaFuncSchema )
2805
+ }
0 commit comments