-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec.go
More file actions
160 lines (140 loc) · 3.49 KB
/
spec.go
File metadata and controls
160 lines (140 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//go:generate go-enum -f=$GOFILE --marshal
package main
import (
"net/http"
"strconv"
"strings"
"github.com/americanas-go/log"
)
// ENUM(HTTP,FUNCTION,GRPC)
type AppType int
// ENUM(INTERFACE,FUNCTION)
type Type int
type AppSpec interface {
SetFromAnnotations(string, []Annotation)
}
type GRPCSpec struct {
Name string
Description string
Options struct {
GoPackage string
JavaPackage string
}
Methods []struct {
Name string
// Type string (UNARY, STREAM)
Handler string
Package string
RelativePackage string
Type Type
Message struct {
Input string
Output string
}
}
}
func (s *GRPCSpec) SetFromAnnotations(handler string, an []Annotation) {}
type FunctionSpec struct {
Name string
Description string
Handler string
Type Type
Package string
RelativePackage string
}
func (s *FunctionSpec) SetFromAnnotations(handler string, an []Annotation) {}
type HTTPSpec struct {
Name string
Description string
Endpoints []EndpointHTTPSpec
}
func (s *HTTPSpec) SetFromAnnotations(handler string, an []Annotation) {
es := EndpointHTTPSpec{
Handler: handler,
Method: http.MethodGet,
}
for _, a := range an {
switch a.AnnotationType {
case AnnotationTypePACKAGE:
es.Package = a.SimpleValue()
case AnnotationTypeRELATIVEPACKAGE:
es.RelativePackage = a.SimpleValue()
case AnnotationTypeTYPE:
v, err := ParseType(strings.ToUpper(a.SimpleValue()))
if err != nil {
log.Warnf("cannot parse http handler type.", err.Error())
}
es.Type = v
case AnnotationTypePATH:
es.Paths = append(es.Paths, strings.ToLower(a.SimpleValue()))
case AnnotationTypeMETHOD:
es.Method = strings.ToUpper(a.SimpleValue())
case AnnotationTypeCONSUME:
es.Consumes = append(es.Consumes, strings.ToLower(a.SimpleValue()))
case AnnotationTypePRODUCE:
es.Produces = append(es.Produces, strings.ToLower(a.SimpleValue()))
case AnnotationTypePARAM:
es.Parameters = append(es.Parameters, NewParameterHTTPSpecFromAnnotation(a))
case AnnotationTypeBODY:
es.Body = a.SimpleValue()
case AnnotationTypeRESPONSE:
es.Responses = append(es.Responses, NewResponseHTTPSpecFromAnnotation(a))
}
}
s.Endpoints = append(s.Endpoints, es)
}
type EndpointHTTPSpec struct {
Paths []string
Method string
Package string
RelativePackage string
Description string
Handler string
Type Type
Consumes []string
Produces []string
Parameters []ParameterHTTPSpec
Responses []ResponseHTTPSpec
Body string
}
type ResponseHTTPSpec struct {
Description string
Schema string
Code int
}
func NewResponseHTTPSpecFromAnnotation(a Annotation) ResponseHTTPSpec {
v := strings.Split(a.Value, " ")
code, _ := strconv.Atoi(v[1])
return ResponseHTTPSpec{
Description: strings.Join(v[3:], " "),
Code: code,
Schema: v[2],
}
}
type ParameterHTTPSpec struct {
Name string
Description string
Source string
Type string
Required bool
Validations struct{}
}
func NewParameterHTTPSpecFromAnnotation(a Annotation) ParameterHTTPSpec {
v := strings.Split(a.Value, " ")
req, _ := strconv.ParseBool(v[4])
return ParameterHTTPSpec{
Name: v[2],
Description: strings.Join(v[5:], " "),
Source: v[1],
Type: v[3],
Required: req,
Validations: struct{}{},
}
}
type Spec struct {
Apps []*AppsSpec
}
type AppsSpec struct {
Type AppType
Spec AppSpec
}