Skip to content

Commit ea09603

Browse files
committedNov 15, 2020
added ObjectIsInput and ObjectIsOutput helpers
1 parent fde060b commit ea09603

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
 

‎parser/parser.go

+30
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ func (d *Definition) Object(name string) (*Object, error) {
4545
return nil, ErrNotFound
4646
}
4747

48+
// ObjectIsInput gets whether this object is a method
49+
// input (request) type or not.\
50+
// Returns true if any method.InputObject.ObjectName matches
51+
// name.
52+
func (d *Definition) ObjectIsInput(name string) bool {
53+
for _, service := range d.Services {
54+
for _, method := range service.Methods {
55+
if method.InputObject.ObjectName == name {
56+
return true
57+
}
58+
}
59+
}
60+
return false
61+
}
62+
63+
// ObjectIsOutput gets whether this object is a method
64+
// output (response) type or not.
65+
// Returns true if any method.OutputObject.ObjectName matches
66+
// name.
67+
func (d *Definition) ObjectIsOutput(name string) bool {
68+
for _, service := range d.Services {
69+
for _, method := range service.Methods {
70+
if method.OutputObject.ObjectName == name {
71+
return true
72+
}
73+
}
74+
}
75+
return false
76+
}
77+
4878
// Service describes a service, akin to an interface in Go.
4979
type Service struct {
5080
Name string `json:"name"`

‎parser/parser_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,18 @@ func TestExtractCommentMetadata(t *testing.T) {
208208
is.Equal(metadata["required"], true)
209209
is.Equal(metadata["monkey"], float64(24))
210210
}
211+
212+
func TestObjectIsInputOutput(t *testing.T) {
213+
is := is.New(t)
214+
patterns := []string{"./testdata/services/pleasantries"}
215+
parser := New(patterns...)
216+
parser.Verbose = testing.Verbose()
217+
parser.ExcludeInterfaces = []string{"Ignorer"}
218+
def, err := parser.Parse()
219+
is.NoErr(err)
220+
221+
is.Equal(def.ObjectIsInput("GreetRequest"), true)
222+
is.Equal(def.ObjectIsInput("GreetResponse"), false)
223+
is.Equal(def.ObjectIsOutput("GreetRequest"), false)
224+
is.Equal(def.ObjectIsOutput("GreetResponse"), true)
225+
}

0 commit comments

Comments
 (0)
Please sign in to comment.