Skip to content
Merged
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
18 changes: 18 additions & 0 deletions pkg/asyncapi/v2/follow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package asyncapiv2

import (
"testing"

"github.com/stretchr/testify/assert"
)

// TestParameterFollow ensures Parameter.Follow returns the referenced parameter
// when set and the parameter itself otherwise (issue #146).
func TestParameterFollow(t *testing.T) {
p := &Parameter{}
assert.Same(t, p, p.Follow())

target := &Parameter{}
ref := &Parameter{ReferenceTo: target}
assert.Same(t, target, ref.Follow())
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v2/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ func (p *Parameter) setDependencies(spec Specification) error {

return nil
}

// Follow returns the referenced Parameter if there is one, otherwise the Parameter itself.
func (p *Parameter) Follow() *Parameter {
if p.ReferenceTo != nil {
return p.ReferenceTo
}
return p
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/channel_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ func (chb *ChannelBindings) setDependencies(spec Specification) error {

return nil
}

// Follow returns the referenced ChannelBindings if there is one, otherwise the ChannelBindings itself.
func (chb *ChannelBindings) Follow() *ChannelBindings {
if chb.ReferenceTo != nil {
return chb.ReferenceTo
}
return chb
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/external_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ func (doc *ExternalDocumentation) setDependencies(spec Specification) error {

return nil
}

// Follow returns the referenced ExternalDocumentation if there is one, otherwise the ExternalDocumentation itself.
func (doc *ExternalDocumentation) Follow() *ExternalDocumentation {
if doc.ReferenceTo != nil {
return doc.ReferenceTo
}
return doc
}
48 changes: 48 additions & 0 deletions pkg/asyncapi/v3/follow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package asyncapiv3

import (
"testing"

"github.com/stretchr/testify/assert"
)

// TestFollowReturnsSelfWithoutReference ensures Follow returns the structure
// itself when it does not reference another one (issue #146).
func TestFollowReturnsSelfWithoutReference(t *testing.T) {
srv := &Server{}
assert.Same(t, srv, srv.Follow())

tag := &Tag{}
assert.Same(t, tag, tag.Follow())

ex := &MessageExample{}
assert.Same(t, ex, ex.Follow())

ora := &OperationReplyAddress{}
assert.Same(t, ora, ora.Follow())

sec := &SecurityScheme{}
assert.Same(t, sec, sec.Follow())

sv := &ServerVariable{}
assert.Same(t, sv, sv.Follow())

doc := &ExternalDocumentation{}
assert.Same(t, doc, doc.Follow())
}

// TestFollowReturnsReference ensures Follow returns the referenced structure
// when one is set (issue #146).
func TestFollowReturnsReference(t *testing.T) {
target := &Server{}
ref := &Server{ReferenceTo: target}
assert.Same(t, target, ref.Follow())

tagTarget := &Tag{}
tagRef := &Tag{ReferenceTo: tagTarget}
assert.Same(t, tagTarget, tagRef.Follow())

secTarget := &SecurityScheme{}
secRef := &SecurityScheme{ReferenceTo: secTarget}
assert.Same(t, secTarget, secRef.Follow())
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/message_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ func (mb *MessageBindings) setDependencies(spec Specification) error {

return nil
}

// Follow returns the referenced MessageBindings if there is one, otherwise the MessageBindings itself.
func (mb *MessageBindings) Follow() *MessageBindings {
if mb.ReferenceTo != nil {
return mb.ReferenceTo
}
return mb
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/message_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ func (me *MessageExample) setDependencies(spec Specification) error {

return nil
}

// Follow returns the referenced MessageExample if there is one, otherwise the MessageExample itself.
func (me *MessageExample) Follow() *MessageExample {
if me.ReferenceTo != nil {
return me.ReferenceTo
}
return me
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/operation_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ func (ob *OperationBindings) setDependencies(spec Specification) error {

return nil
}

// Follow returns the referenced OperationBindings if there is one, otherwise the OperationBindings itself.
func (ob *OperationBindings) Follow() *OperationBindings {
if ob.ReferenceTo != nil {
return ob.ReferenceTo
}
return ob
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/operation_reply_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ func (ora OperationReplyAddress) isLocationRequired(op *Operation) (bool, error)
path := strings.Split(ora.Location, "/")
return locationParent.IsFieldRequired(path[len(path)-1]), nil
}

// Follow returns the referenced OperationReplyAddress if there is one, otherwise the OperationReplyAddress itself.
func (ora *OperationReplyAddress) Follow() *OperationReplyAddress {
if ora.ReferenceTo != nil {
return ora.ReferenceTo
}
return ora
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/security_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,11 @@ func RemoveDuplicateSecuritySchemes(securities []*SecurityScheme) []*SecuritySch
}
return newList
}

// Follow returns the referenced SecurityScheme if there is one, otherwise the SecurityScheme itself.
func (s *SecurityScheme) Follow() *SecurityScheme {
if s.ReferenceTo != nil {
return s.ReferenceTo
}
return s
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,11 @@ func (srv *Server) setReference(spec Specification) error {

return nil
}

// Follow returns the referenced Server if there is one, otherwise the Server itself.
func (srv *Server) Follow() *Server {
if srv.ReferenceTo != nil {
return srv.ReferenceTo
}
return srv
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/server_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ func (ob *ServerBindings) setDependencies(spec Specification) error {

return nil
}

// Follow returns the referenced ServerBindings if there is one, otherwise the ServerBindings itself.
func (ob *ServerBindings) Follow() *ServerBindings {
if ob.ReferenceTo != nil {
return ob.ReferenceTo
}
return ob
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/server_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ func (sv *ServerVariable) setDependencies(spec Specification) error {

return nil
}

// Follow returns the referenced ServerVariable if there is one, otherwise the ServerVariable itself.
func (sv *ServerVariable) Follow() *ServerVariable {
if sv.ReferenceTo != nil {
return sv.ReferenceTo
}
return sv
}
8 changes: 8 additions & 0 deletions pkg/asyncapi/v3/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ func RemoveDuplicateTags(tags []*Tag) []*Tag {
}
return newList
}

// Follow returns the referenced Tag if there is one, otherwise the Tag itself.
func (t *Tag) Follow() *Tag {
if t.ReferenceTo != nil {
return t.ReferenceTo
}
return t
}
Loading