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 method OriginalString #316

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
25 changes: 17 additions & 8 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@
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 @@
}

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,11 @@
return p.hash
}

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

// Types returns the types of the protocol.
func (p *Protocol) Types() []NamedSchema {
return p.types
Expand Down Expand Up @@ -231,7 +240,7 @@
}

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

type protocol struct {
Expand All @@ -243,7 +252,7 @@
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 @@
}
}

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))

Check failure on line 291 in protocol.go

View workflow job for this annotation

GitHub Actions / test (1.21)

line is 123 characters (lll)
}

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
Loading