Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove property reservations #468

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import (
"github.com/mitchellh/mapstructure"
)

var (
protocolReserved = []string{"doc", "types", "messages", "protocol", "namespace"}
messageReserved = []string{"doc", "response", "request", "errors", "one-way"}
)

type protocolConfig struct {
doc string
props map[string]any
Expand Down Expand Up @@ -70,7 +65,7 @@ func NewProtocol(

p := &Protocol{
name: n,
properties: newProperties(cfg.props, protocolReserved),
properties: newProperties(cfg.props),
types: types,
messages: messages,
doc: cfg.doc,
Expand Down Expand Up @@ -145,7 +140,7 @@ func NewMessage(req *RecordSchema, resp Schema, errors *UnionSchema, oneWay bool
}

return &Message{
properties: newProperties(cfg.props, messageReserved),
properties: newProperties(cfg.props),
req: req,
resp: resp,
errs: errors,
Expand Down
10 changes: 10 additions & 0 deletions protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,13 @@ func TestParseProtocol_Types(t *testing.T) {
assert.Equal(t, wantPong, protocol.Types()[1].String())
assert.Equal(t, wantPongError, protocol.Types()[2].String())
}

func TestProtocol_String(t *testing.T) {
protocol, err := avro.ParseProtocolFile("testdata/echo.avpr")
require.NoError(t, err)

json := protocol.String()

want := `{"protocol":"Echo","namespace":"org.hamba.avro","types":[{"name":"org.hamba.avro.Ping","type":"record","fields":[{"name":"timestamp","type":"long"},{"name":"text","type":"string"}]},{"name":"org.hamba.avro.Pong","type":"record","fields":[{"name":"timestamp","type":"long"},{"name":"ping","type":"org.hamba.avro.Ping"}]},{"name":"org.hamba.avro.PongError","type":"error","fields":[{"name":"timestamp","type":"long"},{"name":"reason","type":"string"}]}],"messages":{"ping":{"request":[{"name":"ping","type":"org.hamba.avro.Ping"}],"response":"org.hamba.avro.Pong","errors":["org.hamba.avro.PongError"]}}}`
assert.Equal(t, want, json)
}
42 changes: 9 additions & 33 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ func (nullDefaultType) MarshalJSON() ([]byte, error) {

var nullDefault nullDefaultType = struct{}{}

var (
schemaReserved = []string{
"doc", "fields", "items", "name", "namespace", "size", "symbols",
"values", "type", "aliases", "logicalType", "precision", "scale",
}
fieldReserved = []string{"default", "doc", "name", "order", "type", "aliases"}
)

// Type is a schema type.
type Type string

Expand Down Expand Up @@ -343,24 +335,8 @@ type properties struct {
props map[string]any
}

func newProperties(props map[string]any, res []string) properties {
p := properties{props: map[string]any{}}
for k, v := range props {
if isReserved(res, k) {
continue
}
p.props[k] = v
}
return p
}

func isReserved(res []string, k string) bool {
for _, r := range res {
if k == r {
return true
}
}
return false
func newProperties(props map[string]any) properties {
return properties{props: props}
}

// Prop gets a property from the schema.
Expand Down Expand Up @@ -484,7 +460,7 @@ func NewPrimitiveSchema(t Type, l LogicalSchema, opts ...SchemaOption) *Primitiv
}

return &PrimitiveSchema{
properties: newProperties(cfg.props, schemaReserved),
properties: newProperties(cfg.props),
cacheFingerprinter: cacheFingerprinter{writerFingerprint: cfg.wfp},
typ: t,
logical: l,
Expand Down Expand Up @@ -574,7 +550,7 @@ func NewRecordSchema(name, namespace string, fields []*Field, opts ...SchemaOpti

return &RecordSchema{
name: n,
properties: newProperties(cfg.props, schemaReserved),
properties: newProperties(cfg.props),
cacheFingerprinter: cacheFingerprinter{writerFingerprint: cfg.wfp},
fields: fields,
doc: cfg.doc,
Expand Down Expand Up @@ -743,7 +719,7 @@ func NewField(name string, typ Schema, opts ...SchemaOption) (*Field, error) {
}

f := &Field{
properties: newProperties(cfg.props, fieldReserved),
properties: newProperties(cfg.props),
name: name,
aliases: cfg.aliases,
doc: cfg.doc,
Expand Down Expand Up @@ -919,7 +895,7 @@ func NewEnumSchema(name, namespace string, symbols []string, opts ...SchemaOptio

return &EnumSchema{
name: n,
properties: newProperties(cfg.props, schemaReserved),
properties: newProperties(cfg.props),
cacheFingerprinter: cacheFingerprinter{writerFingerprint: cfg.wfp},
symbols: symbols,
def: def,
Expand Down Expand Up @@ -1072,7 +1048,7 @@ func NewArraySchema(items Schema, opts ...SchemaOption) *ArraySchema {
}

return &ArraySchema{
properties: newProperties(cfg.props, schemaReserved),
properties: newProperties(cfg.props),
cacheFingerprinter: cacheFingerprinter{writerFingerprint: cfg.wfp},
items: items,
}
Expand Down Expand Up @@ -1142,7 +1118,7 @@ func NewMapSchema(values Schema, opts ...SchemaOption) *MapSchema {
}

return &MapSchema{
properties: newProperties(cfg.props, schemaReserved),
properties: newProperties(cfg.props),
cacheFingerprinter: cacheFingerprinter{writerFingerprint: cfg.wfp},
values: values,
}
Expand Down Expand Up @@ -1325,7 +1301,7 @@ func NewFixedSchema(

return &FixedSchema{
name: n,
properties: newProperties(cfg.props, schemaReserved),
properties: newProperties(cfg.props),
cacheFingerprinter: cacheFingerprinter{writerFingerprint: cfg.wfp},
size: size,
logical: logical,
Expand Down
3 changes: 3 additions & 0 deletions schema_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func parseComplexType(namespace string, m map[string]any, seen seenCache, cache
}

type primitiveSchema struct {
Type string `mapstructure:"type"`
LogicalType string `mapstructure:"logicalType"`
Precision int `mapstructure:"precision"`
Scale int `mapstructure:"scale"`
Expand Down Expand Up @@ -368,6 +369,7 @@ func parseEnum(namespace string, m map[string]any, seen seenCache, cache *Schema
}

type arraySchema struct {
Type string `mapstructure:"type"`
Items any `mapstructure:"items"`
Props map[string]any `mapstructure:",remain"`
}
Expand All @@ -393,6 +395,7 @@ func parseArray(namespace string, m map[string]any, seen seenCache, cache *Schem
}

type mapSchema struct {
Type string `mapstructure:"type"`
Values any `mapstructure:"values"`
Props map[string]any `mapstructure:",remain"`
}
Expand Down
Loading