Skip to content

Commit

Permalink
Add full standard json codec support (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrflatt authored Jun 22, 2024
1 parent be49c89 commit cf0552b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/riferrei/srclient
go 1.12

require (
github.com/linkedin/goavro/v2 v2.11.1
github.com/linkedin/goavro/v2 v2.12.0
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0
github.com/stretchr/testify v1.7.1
github.com/stretchr/testify v1.7.5
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/linkedin/goavro/v2 v2.11.1 h1:4cuAtbDfqkKnBXp9E+tRkIJGa6W6iAjwonwt8O1f4U0=
github.com/linkedin/goavro/v2 v2.11.1/go.mod h1:UgQUb2N/pmueQYH9bfqFioWxzYCZXSfF8Jw03O5sjqA=
github.com/linkedin/goavro/v2 v2.12.0/go.mod h1:KXx+erlq+RPlGSPmLF7xGo6SAbh8sCQ53x064+ioxhk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 h1:TToq11gyfNlrMFZiYujSekIsPd9AmsA2Bj/iv+s4JHE=
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 h1:w8s32wxx3sY+OjLlv9qltkLU5yvJzxjjgiHWLjdIcw4=
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
25 changes: 22 additions & 3 deletions schemaRegistryClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type SchemaRegistryClient struct {
cachingEnabled bool
cachingEnabledLock sync.RWMutex
codecCreationEnabled bool
codecAsFullJson bool
codecCreationEnabledLock sync.RWMutex
idSchemaCache map[int]*Schema
idSchemaCacheLock sync.RWMutex
Expand Down Expand Up @@ -283,7 +284,7 @@ func (client *SchemaRegistryClient) GetSchema(schemaID int) (*Schema, error) {

var codec *goavro.Codec
if client.getCodecCreationEnabled() {
codec, err = goavro.NewCodec(schemaResp.Schema)
codec, err = client.getCodecForSchema(schemaResp.Schema)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -530,7 +531,7 @@ func (client *SchemaRegistryClient) LookupSchema(subject string, schema string,

var codec *goavro.Codec
if client.getCodecCreationEnabled() && schemaType == Avro {
codec, err = goavro.NewCodec(schemaResp.Schema)
codec, err = client.getCodecForSchema(schemaResp.Schema)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -662,6 +663,15 @@ func (client *SchemaRegistryClient) CodecCreationEnabled(value bool) {
client.codecCreationEnabled = value
}

// CodecJsonEnabled allows the application to create codec,
// which will serialize/deserialize data as standard json.
// Should be used with CodecCreationEnabled, otherwise it will be ignored.
func (client *SchemaRegistryClient) CodecJsonEnabled(value bool) {
client.codecCreationEnabledLock.Lock()
defer client.codecCreationEnabledLock.Unlock()
client.codecAsFullJson = value
}

func (client *SchemaRegistryClient) getVersion(subject string, version string) (*Schema, error) {

if client.getCachingEnabled() {
Expand All @@ -686,7 +696,7 @@ func (client *SchemaRegistryClient) getVersion(subject string, version string) (
}
var codec *goavro.Codec
if client.getCodecCreationEnabled() {
codec, err = goavro.NewCodec(schemaResp.Schema)
codec, err = client.getCodecForSchema(schemaResp.Schema)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -763,6 +773,15 @@ func (client *SchemaRegistryClient) getCodecCreationEnabled() bool {
return client.codecCreationEnabled
}

func (client *SchemaRegistryClient) getCodecForSchema(schema string) (*goavro.Codec, error) {
client.codecCreationEnabledLock.RLock()
defer client.codecCreationEnabledLock.RUnlock()
if client.codecAsFullJson {
return goavro.NewCodecForStandardJSONFull(schema)
}
return goavro.NewCodec(schema)
}

// NewSchema instantiates a new Schema struct.
func NewSchema(
id int,
Expand Down

0 comments on commit cf0552b

Please sign in to comment.