Skip to content

Commit

Permalink
Added method OriginalString
Browse files Browse the repository at this point in the history
  • Loading branch information
Elia Bracci committed Oct 20, 2023
1 parent 1052b7d commit 083c889
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
25 changes: 17 additions & 8 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ type Protocol struct {
doc string

hash string

originalString string
}

// NewProtocol creates a protocol instance.
func NewProtocol(
name, namepsace string,
types []NamedSchema,
messages map[string]*Message,
protocolString string,
opts ...ProtocolOption,
) (*Protocol, error) {
var cfg protocolConfig
Expand All @@ -69,11 +72,12 @@ func NewProtocol(
}

p := &Protocol{
name: n,
properties: newProperties(cfg.props, protocolReserved),
types: types,
messages: messages,
doc: cfg.doc,
name: n,
properties: newProperties(cfg.props, protocolReserved),
types: types,
messages: messages,
doc: cfg.doc,
originalString: protocolString,
}

b := md5.Sum([]byte(p.String()))
Expand Down Expand Up @@ -102,6 +106,11 @@ func (p *Protocol) Types() []NamedSchema {
return p.types
}

// OriginalString returns the original string of the protocol.
func (p *Protocol) OriginalString() string {
return p.originalString
}

// String returns the canonical form of the protocol.
func (p *Protocol) String() string {
types := ""
Expand Down Expand Up @@ -231,7 +240,7 @@ func ParseProtocol(protocol string) (*Protocol, error) {
}

seen := seenCache{}
return parseProtocol(m, seen, cache)
return parseProtocol(m, seen, cache, protocol)
}

type protocol struct {
Expand All @@ -243,7 +252,7 @@ type protocol struct {
Props map[string]any `mapstructure:",remain"`
}

func parseProtocol(m map[string]any, seen seenCache, cache *SchemaCache) (*Protocol, error) {
func parseProtocol(m map[string]any, seen seenCache, cache *SchemaCache, protocolString string) (*Protocol, error) {
var (
p protocol
meta mapstructure.Metadata
Expand Down Expand Up @@ -279,7 +288,7 @@ func parseProtocol(m map[string]any, seen seenCache, cache *SchemaCache) (*Proto
}
}

return NewProtocol(p.Protocol, p.Namespace, types, messages, WithProtoDoc(p.Doc), WithProtoProps(p.Props))
return NewProtocol(p.Protocol, p.Namespace, types, messages, protocolString, WithProtoDoc(p.Doc), WithProtoProps(p.Props))
}

func parseProtocolTypes(namespace string, types []any, seen seenCache, cache *SchemaCache) ([]NamedSchema, error) {
Expand Down
11 changes: 10 additions & 1 deletion protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ func TestMustParseProtocol_PanicsOnError(t *testing.T) {
}

func TestNewProtocol_ValidatesName(t *testing.T) {
_, err := avro.NewProtocol("0test", "", nil, nil)
_, err := avro.NewProtocol("0test", "", nil, nil, "")

assert.Error(t, err)
}

func TestNewProtocol_OriginalString(t *testing.T) {
schema := `{"protocol":"test", "namespace": "org.hamba.avro", "messages":{"test":{"request": [{"name": "foobar", "type": "string"}]}}}`

proto, err := avro.ParseProtocol(schema)

assert.NoError(t, err)
assert.Equal(t, schema, proto.OriginalString())
}

func TestNewMessage(t *testing.T) {
field, _ := avro.NewField("test", avro.NewPrimitiveSchema(avro.String, nil))
fields := []*avro.Field{field}
Expand Down

0 comments on commit 083c889

Please sign in to comment.