Skip to content

Commit cc09090

Browse files
committed
add post-processing for method names
1 parent 93933e1 commit cc09090

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

parser.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,43 @@ func ParseFile(filePath string) (*APIDefinition, error) {
116116
return nil, ramlError
117117
}
118118

119+
postProcess(apiDefinition)
120+
119121
// Good.
120122
return apiDefinition, nil
121123
}
122124

125+
func postProcess(d *APIDefinition) {
126+
for _, r := range d.Resources {
127+
addMethodNames(&r)
128+
}
129+
}
130+
131+
func addMethodNames(r *Resource) {
132+
if r.Get != nil {
133+
r.Get.Name = "GET"
134+
}
135+
if r.Post != nil {
136+
r.Post.Name = "POST"
137+
}
138+
if r.Put != nil {
139+
r.Put.Name = "PUT"
140+
}
141+
if r.Patch != nil {
142+
r.Patch.Name = "PATCH"
143+
}
144+
if r.Head != nil {
145+
r.Head.Name = "HEAD"
146+
}
147+
if r.Delete != nil {
148+
r.Delete.Name = "DELETE"
149+
}
150+
151+
for _, n := range r.Nested {
152+
addMethodNames(n)
153+
}
154+
}
155+
123156
// Reads the contents of a file, returns a bytes buffer
124157
func readFileContents(workingDirectory string, fileName string) ([]byte, error) {
125158

raml_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,21 @@ func TestParsing(t *testing.T) {
9090
// pretty.Println(apiDefinition)
9191
}
9292
}
93+
94+
func TestMethodStringer(t *testing.T) {
95+
def, _ := ParseFile("./samples/simple_example.raml")
96+
r := def.Resources["/resources"]
97+
if r.Get.Name != "GET" {
98+
t.Errorf("Got %s, instead of GET", r.Get.Name)
99+
}
100+
n := r.Nested["/{resourceId}"]
101+
if n.Get.Name != "GET" {
102+
t.Errorf("Got %s, instead of GET", n.Get.Name)
103+
}
104+
if n.Put.Name != "PUT" {
105+
t.Errorf("Got %s, instead of PUT", n.Put.Name)
106+
}
107+
if n.Delete.Name != "DELETE" {
108+
t.Errorf("Got %s, instead of DELETE", n.Delete.Name)
109+
}
110+
}

types.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,6 @@ type Trait struct {
366366
// doesn't contain Usage, optional fields etc.
367367
type ResourceTypeMethod struct {
368368
Name string
369-
// TODO: Fill this during the post-processing phase
370369

371370
// Briefly describes what the method does to the resource
372371
Description string
@@ -531,7 +530,6 @@ type SecurityScheme struct {
531530
// Methods are operations that are performed on a resource
532531
type Method struct {
533532
Name string
534-
// TODO: Fill this during the post-processing phase
535533

536534
// Briefly describes what the method does to the resource
537535
Description string

0 commit comments

Comments
 (0)