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

Added Types methods to Protocol #315

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 22 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,
elia-bracci-hs marked this conversation as resolved.
Show resolved Hide resolved
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 All @@ -97,6 +101,16 @@ func (p *Protocol) Hash() string {
return p.hash
}

// Types returns the types of the protocol.
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 @@ -226,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 @@ -238,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 @@ -274,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
25 changes: 24 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 Expand Up @@ -190,3 +199,17 @@ func TestParseProtocolFile_InvalidPath(t *testing.T) {

assert.Error(t, err)
}

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

wantPing := `{"name":"org.hamba.avro.Ping","type":"record","fields":[{"name":"timestamp","type":"long"},{"name":"text","type":"string"}]}`
wantPong := `{"name":"org.hamba.avro.Pong","type":"record","fields":[{"name":"timestamp","type":"long"},{"name":"ping","type":"org.hamba.avro.Ping"}]}`
wantPongError := `{"name":"org.hamba.avro.PongError","type":"error","fields":[{"name":"timestamp","type":"long"},{"name":"reason","type":"string"}]}`
wantLen := 3
require.NoError(t, err)
assert.Equal(t, wantLen, len(protocol.Types()))
assert.Equal(t, wantPing, protocol.Types()[0].String())
assert.Equal(t, wantPong, protocol.Types()[1].String())
assert.Equal(t, wantPongError, protocol.Types()[2].String())
}