Skip to content

Commit

Permalink
feat: added Types methods to Protocol (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
elia-bracci-hs committed Oct 20, 2023
1 parent 3ac44d5 commit 50a7897
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (p *Protocol) Hash() string {
return p.hash
}

// Types returns the types of the protocol.
func (p *Protocol) Types() []NamedSchema {
return p.types
}

// String returns the canonical form of the protocol.
func (p *Protocol) String() string {
types := ""
Expand Down
14 changes: 14 additions & 0 deletions protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,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())
}

0 comments on commit 50a7897

Please sign in to comment.