-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
144 lines (127 loc) · 4.02 KB
/
websocket.go
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
package generator
import "github.com/dave/jennifer/jen"
func (g *generator) generateWebsocketApi() (err error) {
at := g.newFile()
var invokeCaseStatements []jen.Code
var interfaceMethods []jen.Code
var decodeCaseStatements []jen.Code
// generate individual structs, capturing case statements for api
for _, m := range g.spec.Methods {
interfaceMethods = append(interfaceMethods,
jen.Id(m.Name).Params(
jen.Id("clientId").String(),
jen.Id("request").Op("*").Qual(g.qualifier, m.Request),
).Params(
jen.Id("response").Op("*").Qual(g.qualifier, m.Response),
jen.Err().Error(),
),
)
invokeCaseStatements = append(invokeCaseStatements,
jen.Case(jen.Lit(m.Request)).Block(
jen.Id("request").Op(":=&").Id(m.Request).Op("{}"),
jen.If(jen.Err().Op("=").Qual("encoding/json", "Unmarshal").
Params(
jen.Id("messageJson"),
jen.Id("request"),
).Op(";").Err().Op("!=").Nil()).Block(jen.Return(jen.Nil(), jen.Err())),
jen.Var().Id("response").Op("*").Qual(g.qualifier, m.Response),
jen.If(jen.Id("response").Op(",").Err().Op("=").Id("api").Dot(m.Name)).
Params(
jen.Id("clientId"),
jen.Id("request"),
).Op(";").Err().Op("!=").Nil()).Block(jen.Return(jen.Nil(), jen.Err())),
jen.Return(jen.Id("response").Dot("WebSocketMessage").Params()),
)
decodeCaseStatements = append(decodeCaseStatements,
jen.Case(jen.Lit(m.Request)).Block(
jen.Id("result").Op("=&").Qual(g.qualifier, m.Request).Op("{}"),
),
jen.Case(jen.Lit(m.Response)).Block(
jen.Id("result").Op("=&").Qual(g.qualifier, m.Response).Op("{}"),
),
)
}
decodeCaseStatements = append(decodeCaseStatements,
jen.Default().Block(
jen.Return(jen.Nil(), jen.Qual("fmt", "Errorf").Params(
jen.Lit("message type %q not mapped"),
jen.Id("messageType"),
)),
),
)
at.Type().Id("Api").Interface(interfaceMethods...)
at.Line()
at.Type().Id("WebSocketMessage").Interface(
jen.Id("WebSocketMessage").Params().Params(jen.Op("[]").Byte(), jen.Error()),
jen.Id("WebSocketMessageName").Params().Params(jen.String()),
)
at.Line()
at.Func().Id("InvokeApi").
Params(
jen.Id("clientId").String(),
jen.Id("data").Op("[]").Byte(),
jen.Id("api").Qual(g.qualifier, "Api")).
Params(
jen.Id("result").Op("[]").Byte(),
jen.Err().Error(),
).Block(
jen.Var().Id("messageType").String(),
jen.Var().Id("messageJson").Op("[]").Byte(),
jen.For(
jen.Id("i").Op(":=").Lit(0),
jen.Id("i").Op("<").Len(jen.Id("data")),
jen.Id("i").Op("++"),
).Block(
jen.If(jen.Id("data").Op("[").Id("i").Op("]==").Lit(0)).Block(
jen.Id("messageType").Op("=").String().Params(
jen.Id("data").Op("[").Lit(0).Op(":").Id("i").Op("]"),
),
jen.Id("messageJson").Op("=").Id("data").Op("[").Id("i").Op("+1:]"),
jen.Break(),
),
),
jen.Line(),
jen.Switch(jen.Id("messageType")).Block(invokeCaseStatements...),
jen.Line(),
jen.Return(jen.Nil(), jen.Qual("fmt", "Errorf").Params(
jen.Lit("message type %q not mapped"),
jen.Id("messageType"),
)),
)
at.Line()
at.Func().Id("DecodeMessage").Params(
jen.Id("data").Op("[]").Byte(),
).Params(
jen.Id("result").Qual(g.qualifier, "WebSocketMessage"),
jen.Err().Error(),
).Block(
jen.Var().Id("messageType").String(),
jen.Var().Id("messageJson").Op("[]").Byte(),
at.Line(),
jen.For(
jen.Id("i").Op(":=").Lit(0),
jen.Id("i").Op("<").Len(jen.Id("data")),
jen.Id("i").Op("++"),
).Block(
jen.If(jen.Id("data").Op("[").Id("i").Op("]==").Lit(0)).Block(
jen.Id("messageType").Op("=").String().Params(
jen.Id("data").Op("[").Lit(0).Op(":").Id("i").Op("]"),
),
jen.Id("messageJson").Op("=").Id("data").Op("[").Id("i").Op("+1:]"),
jen.Break(),
),
),
at.Line(),
jen.Switch(jen.Id("messageType")).Block(decodeCaseStatements...),
jen.If(jen.Err().Op("=").Qual("encoding/json", "Unmarshal").
Params(
jen.Id("messageJson"),
jen.Id("result"),
).Op(";").Err().Op("!=").Nil()).
Block(
jen.Return(jen.Nil(), jen.Err()),
),
jen.Return(),
)
return g.render("api", at)
}