Newer
Older
package parse
import (
"strings"
"gfx.cafe/open/jrpc/openrpc/types"
"gfx.cafe/open/jrpc/openrpc/util"
"github.com/go-openapi/spec"
)
type GoGlobal struct {
Structs map[string]*GoStruct
Types map[string]*GoType
Methods map[string]*GoMethod
}
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
type GoStruct struct {
Name string
Description string
Fields map[string]string
}
type GoField struct {
Name string
Type *GoType
Optional bool
Description string
}
type GoType struct {
Name string
Type string
Description string
}
type GoMethod struct {
Input string
Output string
Name string
}
func (g *GoGlobal) ReadOpenRPC(s types.OpenRPCSpec1) {
for k, v := range s.Components.Schemas {
g.AddSchema(k, v)
}
for _, v := range s.Methods {
g.AddMethod(v.Name, v)
}
}
func (g *GoGlobal) AddSchema(name string, m spec.Schema) {
if len(m.Type) > 0 {
switch m.Type[0] {
case "string":
case "number":
case "boolean":
case "object":
case "array":
default:
}
return
}
if len(m.OneOf) > 0 {
//todo: handle
return
}
}
func (g *GoGlobal) AddMethod(name string, m types.Method) {
inStruct := "Params" + util.CamelCase(name)
outStruct := ""
if m.Result != nil {
outStruct = "Result" + util.CamelCase(name)
}
g.Methods[name] = &GoMethod{
Input: inStruct,
Output: outStruct,
Name: util.CamelCase(name),
}
}
func (g *GoGlobal) AddStruct(name string, m spec.Schema) {
s := &GoStruct{}
g.Structs[name] = s
s.Name = name
s.Description = m.Description
s.Fields = map[string]string{}
for k, v := range m.Properties {
g.AddSchema(k, v)
if ref := v.Ref.GetPointer(); ref != nil {
refstr := strings.ToTitle(ref.String())
s.Fields[k] = refstr
} else {
if len(v.Type) > 0 {
refstr := v.Type[0]
s.Fields[k] = refstr
}
}
}
}