-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonmsg.go
688 lines (598 loc) · 15.4 KB
/
jsonmsg.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
Package jsonmsg parses API specs.
The resulting spec can be used to generate server/client source code in any supported language.
The jsonmsg implementation is based on https://github.com/jsonmsg/spec with the meta-schema https://github.com/jsonmsg/spec/blob/master/meta.json
Generators
go: https://godoc.org/github.com/tfkhsr/jsonmsg/golang
Parse a spec:
schema := `
{
"endpoints": {
"http": "https://jsonmsg.github.io/v1",
"websocket": "wss://jsonmsg.github.io/v1"
},
"messages": {
"findUser": {
"in": "#/definitions/userQuery",
"outs": [
"#/definitions/user",
"#/definitions/error"
],
"group": "user"
}
},
"definitions": {
"user": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": ["id", "name"]
},
"userQuery": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"required": ["id"]
},
"error": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"required": ["message"]
}
}
}
`
// parse spec
spc, err := Parse(spec)
if err != nil {
panic(err)
}
// spc now contains:
// spc.Messages["findUser"] : *Message{...}
// spc.Definitions["user"] : *jsonschema.Schema{...}
*/
package jsonmsg
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"regexp"
"strings"
"text/template"
"github.com/tfkhsr/jsonschema"
)
// A Spec holds endpoints, messages and definitions of an API.
// Parsed spec must validate against meta-schema https://github.com/jsonmsg/spec/blob/master/meta.json
type Spec struct {
// General title of API
Title string
// Further information
Description string
// Map of protocols to URLs (urlString embeds url.URL for unmarshaling)
Endpoints map[string]*urlString
// Map of message names to Messages
Messages map[string]*Message
// Map of groups of message names to Messages
GroupedMessages map[string]map[string]*Message
// JSON Schema definitions for data definition
Definitions jsonschema.Index `json:"-"`
// Raw spec
Raw []byte
}
type urlString struct {
url.URL
}
func (s *urlString) UnmarshalJSON(b []byte) error {
us := string(b)
u, err := url.Parse(us[1 : len(us)-1])
if err != nil {
return err
}
*s = urlString{*u}
return nil
}
// A Message holds details about name, in- and out parameters
type Message struct {
// Short description
Title string
// Detailed description
Description string
// Literal message name as defined in spec
Msg string
// Camel-Cased name
Name string
// JSON Pointer to input schema
In string
// JSON Pointers to possible output schemas
Outs []string
// Pointer to parsed input schema
InSchema *jsonschema.Schema
// Pointers to parsed output schemas
OutSchemas []*jsonschema.Schema
// Pointer to parent spec
Spec *Spec
// Optional: Group name associating message to spec.GroupedMessages
Group string
}
// Parses a raw schema into a Spec
func Parse(b []byte) (*Spec, error) {
var spec Spec
err := json.Unmarshal(b, &spec)
if err != nil {
return nil, err
}
// raw
spec.Raw = b
// endpoints
for k, _ := range spec.Endpoints {
spec.Endpoints[k].Path += "/" + k
}
// definitions
idx, err := jsonschema.Parse(b)
if err != nil {
return nil, err
}
spec.Definitions = *idx
// messages
spec.GroupedMessages = make(map[string]map[string]*Message)
for k, _ := range spec.Messages {
spec.Messages[k].Msg = k
spec.Messages[k].Name = goNameFromStrings(k)
spec.Messages[k].Spec = &spec
InSchema, err := resolvePointerToSchema(spec.Messages[k].In, &spec.Definitions)
if err != nil {
return nil, err
}
spec.Messages[k].InSchema = InSchema
spec.Messages[k].OutSchemas = make([]*jsonschema.Schema, 0)
for i, _ := range spec.Messages[k].Outs {
outSchema, err := resolvePointerToSchema(spec.Messages[k].Outs[i], &spec.Definitions)
if err != nil {
return nil, err
}
spec.Messages[k].OutSchemas = append(spec.Messages[k].OutSchemas, outSchema)
}
if _, ok := spec.GroupedMessages[spec.Messages[k].Group]; !ok {
spec.GroupedMessages[spec.Messages[k].Group] = make(map[string]*Message)
}
spec.GroupedMessages[spec.Messages[k].Group][k] = spec.Messages[k]
}
return &spec, nil
}
// Returns an HTML website version of the spec that must be served on {{ BaseURL }}/spec by servers
func (s *Spec) HTTPSpec() ([]byte, error) {
w := &bytes.Buffer{}
err := writeTemplate(s, httpSpecTemplate, w)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
// Returns an indented, marshalled version of the spec that must be served on {{ BaseURL }}/spec.json by servers
func (s *Spec) JSONSpec() ([]byte, error) {
var o interface{}
err := json.Unmarshal(s.Raw, &o)
if err != nil {
return nil, err
}
return json.MarshalIndent(o, "", " ")
}
// Creates a new message instance conforming to the message schema
func (m *Message) NewInstance() (interface{}, error) {
nm := make(map[string]interface{})
nm["msg"] = m.Msg
if m.InSchema == nil {
return nm, nil
}
data, err := m.InSchema.NewInstance(&m.Spec.Definitions)
if err != nil {
return nil, err
}
nm["data"] = data
return nm, nil
}
// creates a go friendly name from string parts
func goNameFromStrings(parts ...string) string {
name := ""
re := regexp.MustCompile("{|}|-|_")
for _, p := range parts {
c := re.ReplaceAllString(p, "")
switch c {
case "id":
name += "ID"
case "url":
name += "URL"
case "api":
name += "API"
default:
name += strings.Title(c)
}
}
return name
}
// returns a schema or referenced schema
func resolvePointerToSchema(p string, idx *jsonschema.Index) (*jsonschema.Schema, error) {
if p == "" {
return nil, nil
}
s, ok := (*idx)[p]
if !ok {
return nil, fmt.Errorf("jsonmsg: %v does not exist in index", p)
}
return s, nil
}
// Parses the spec input, applies the template and writes it to the writer
func writeTemplate(s *Spec, t string, w io.Writer) error {
tmpl, err := template.New("spec").Funcs(template.FuncMap{
"Contains": stringsContain,
"Add": func(a int, b int) int {
return a + b
},
"JSON": func(in interface{}) string {
jsn, err := json.MarshalIndent(in, "", " ")
if err != nil {
panic(err)
}
return string(jsn)
},
"SubstringRight": func(a string, n int) string {
return a[0 : len(a)-n]
},
}).Parse(t)
if err != nil {
return err
}
err = tmpl.Execute(w, s)
if err != nil {
return err
}
return nil
}
const httpSpecTemplate = `
<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, sans-serif;
color: #333333;
margin: 1.5em;
}
h1 {
color: #3b4151;
}
h2 {
color: #3b4151;
padding-top: 1em;
font-size: 1.2em;
}
h3 {
color: #3b4151;
background: rgba(97,175,254,.1);
padding: 1em;
margin-top: 2em;
}
h3 span {
padding-left: 1em;
font-size: 1em;
font-weight: normal;
}
h4 {
color: #555d73;
padding: 1em;
margin: 0;
margin-top: 2em;
}
dd {
margin-left: 1em;
line-height: 1.4em;
}
dd.level1 {
margin-left: 2em;
}
dd.level2 {
margin-left: 3em;
}
a {
color: #3b4151;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
p.level1 {
padding-left: 1em;
padding-right: 1em;
}
p.level2 {
padding-left: 2em;
padding-right: 1em;
}
.row {
display: flex;
max-width: 50em;
}
.row.right {
justify-content: flex-end;
}
.row div {
display: flex;
padding: 1em;
}
.row button {
margin-top: 1em;
margin-bottom: 1em;
margin-left: 1em;
}
span.property {
font-weight: bold;
}
span.type {
color: #3b4151;
}
span.required {
color: #737373;
padding-left: 0.5em;
}
pre.code,
textarea.code {
width: 100%;
border: none;
background: #EFEFEF;
padding: 1em;
overflow-y: scroll;
margin: 0;
margin-left: 1em;
color: inherit;
}
pre.invisible {
display: none;
}
pre.bg-success {
background: rgba(97, 254, 153, 0.29);
}
pre.bg-error {
background: rgba(254, 97, 97, 0.18);
}
</style>
<script>
var jsonmsg = {
{{ if (index .Endpoints "http") }}
http: {
endpoint: "{{ js (index .Endpoints "http").String }}",
send: function(msg) {
return fetch(jsonmsg.http.endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(msg)
})
.then(function(response) {
return response.json();
});
},
sendFromInputToOutput: function(inId, outId) {
var msg = document.querySelector(inId).value;
var out = document.querySelector(outId);
out.innerHTML = "sending ...";
jsonmsg.http.send(JSON.parse(msg))
.then(function(m){
out.innerHTML = JSON.stringify(m, null, 2);
out.classList = "code bg-success";
})
.catch(function(e) {
out.innerHTML = e;
out.classList = "code bg-error";
});
}
},
{{ end }}
{{ if (index .Endpoints "websocket") }}
websocket: {
endpoint: "{{ js (index .Endpoints "websocket").String }}",
conn: undefined,
connect: function() {
jsonmsg.websocket.conn = new WebSocket(jsonmsg.websocket.endpoint);
},
send: function(msg) {
if(!jsonmsg.websocket.conn) {
jsonmsg.websocket.connect();
}
if (jsonmsg.websocket.conn.readyState !== 1) {
jsonmsg.websocket.conn.addEventListener("open", function(){
jsonmsg.websocket.conn.send(JSON.stringify(msg));
}, { once: true });
} else {
jsonmsg.websocket.conn.send(JSON.stringify(msg));
}
},
sendFromInputToOutput: function(inId, outId) {
var msg = document.querySelector(inId).value;
var out = document.querySelector(outId);
out.innerHTML = "sending ...";
if(!jsonmsg.websocket.conn) {
jsonmsg.websocket.connect();
}
jsonmsg.websocket.conn.addEventListener("message", function(m){
out.innerHTML = m.data;
out.classList = "code bg-success";
}, { once: true });
jsonmsg.websocket.send(JSON.parse(msg));
}
},
{{ end }}
};
</script>
</head>
<body>
<h1>{{ .Title }}</h1>
<p>{{ .Description }}</p>
<a name="index"></a>
<h2>Index</h2>
<dl>
<dd><a href="#specs">Specs</a></dd>
<dd class="level1"><a href="#spec-json">json</a></dd>
<dd class="level1"><a href="#spec-html">html</a></dd>
<dd><a href="#endpoints">Endpoints</a></dd>
{{ range $k, $v := .Endpoints }}
<dd class="level1"><a href="#endpoint-{{ $k }}">{{ $k }}</a></dd>
{{ end }}
<dd><a href="#messages">Messages</a></dd>
{{ range $g, $ms := .GroupedMessages }}
<dd class="level1"><a href="#group-{{ $g }}">{{ $g }}</a></dd>
{{ range $k, $v := $ms }}
<dd class="level2"><a href="#message-{{ $k }}">{{ $k }}</a></dd>
{{ end }}
{{ end }}
<dd><a href="#data">Data</a></dd>
{{ range $k, $v := .Definitions }}
{{ if or (eq $v.Type "object") (eq $v.Type "array") }}
<dd class="level1"><a href="#data-{{ $v.PointerName }}">{{ $v.PointerName }}</a></dd>
{{ end }}
{{ end }}
</dl>
<a name="specs"></a>
<h2>Specs</h2>
<a name="spec-json"></a>
<h3>
json
<span><a href="{{ SubstringRight .Endpoints.http.String 5 }}/spec.json">{{ SubstringRight .Endpoints.http.String 5 }}/spec.json</a></span>
</h3>
<p class="level1">Machine readable spec for API</p>
<a name="spec-html"></a>
<h3>
html
<span><a href="{{ SubstringRight .Endpoints.http.String 5 }}/spec">{{ SubstringRight .Endpoints.http.String 5 }}/spec</a></span>
</h3>
<p class="level1">Human readable spec for API</p>
<a name="endpoints"></a>
<h2>Endpoints</h2>
{{ range $k, $v := .Endpoints }}
<a name="endpoint-{{ $k }}"></a>
<h3>
{{ $k }}
<span>{{ $v.String }}</span>
</h3>
{{ end }}
<a name="messages"></a>
<h2>Messages</h2>
{{ range $g, $ms := .GroupedMessages }}
<a name="group-{{ $g }}"></a>
{{ range $k, $v := $ms }}
<a name="message-{{ $k }}"></a>
<h3>
{{ $k }}
{{ if $g }}<span>{{ $g }}</span>{{ end }}
<span>{{ $v.Title }}</span>
</h3>
<p class="level1">{{ $v.Description }}</p>
<dl>
<dd>In</dd>
{{ if $v.InSchema }}
<dd class="level1"><a href="#data-{{ $v.InSchema.PointerName }}">{{ $v.InSchema.PointerName }}</a></dd>
{{ else }}
<dd class="level1">None</dd>
{{ end }}
<dd>Outs</dd>
{{ range $v.OutSchemas }}
<dd class="level1"><a href="#data-{{ .PointerName }}">{{ .PointerName }}</a></dd>
{{ end }}
</dl>
<h4>Test</h4>
<div class="row">
<textarea class="code" id="input-{{ $k }}" spellcheck="false" rows={{ if not $v.InSchema }}5{{ else }}{{ Add 5 (len $v.InSchema.Properties) }}{{ end }}>{{ JSON $v.NewInstance }}</textarea>
</div>
<div class="row right">
{{ if (index $.Endpoints "websocket") }}
<button onclick="jsonmsg.websocket.sendFromInputToOutput('#input-{{ $k }}', '#output-{{ $k }}')">Send WebSocket</button>
{{ end }}
{{ if (index $.Endpoints "http") }}
<button onclick="jsonmsg.http.sendFromInputToOutput('#input-{{ $k }}', '#output-{{ $k }}')">Send HTTP</button>
{{ end }}
</div>
<div class="row">
<pre class="code invisible" id="output-{{ $k }}"></pre>
</div>
{{ end }}
{{ end }}
<a name="data"></a>
<h2>Data</h2>
{{ range $k, $v := .Definitions }}
{{ if or (eq $v.Type "object") (eq $v.Type "array") }}
<a name="data-{{ $v.PointerName }}"></a>
<h3>
{{ $v.PointerName }}
<span>{{ $v.Type }}</span>
<span>{{ $v.Title }}</span>
</h3>
<p class="level1">{{ $v.Description }}</p>
{{ if eq $v.Type "array" }}
<div class="row">
<div style="width: 33%"><span class="property">Items</span></div>
<div style="width: 33%"><span class="type">
{{ if eq $v.Items.Type "ref" }}
<a href="#data-{{ (index $.Definitions $v.Items.Ref).PointerName }}">{{ (index $.Definitions $v.Items.Ref).PointerName }}</a>
{{ else if or (eq $v.Type "object") (eq $v.Type "array") }}
<a href="#data-{{ $v.PointerName }}">{{ $v.PointerName }}/items</a>
{{ else }}
{{ $v.Items.Type }}
{{ end }}
</span></div>
<div style="width: 33%">{{ $v.Items.Description }}</div>
</div>
{{ end }}
{{ if eq $v.Type "object" }}
<h4>Properties</h4>
{{ range $pk, $pv := $v.Properties }}
<div class="row">
<div style="width: 33%">
<span class="property">{{ $pk }}</span>
{{ if Contains $v.Required $pk }}
<span class="required">(required)</span>
{{ end }}
</div>
<div style="width: 33%"><span class="type">
{{ if eq $pv.Type "ref" }}
<a href="#data-{{ (index $.Definitions $pv.Ref).PointerName }}">{{ (index $.Definitions $pv.Ref).PointerName }}</a>
{{ else if or (eq $pv.Type "object") (eq $pv.Type "array") }}
<a href="#data-{{ $pv.PointerName }}">{{ $pv.PointerName}}</a>
{{ else }}
{{ $pv.Type }}
{{ end }}
</span></div>
<div style="width: 33%">{{ $pv.Description }}</div>
</div>
{{ end }}
{{ end }}
{{ end }}
{{ end }}
</body>
</html>
`
// Checks if a slice of strings contains a string
func stringsContain(l []string, s string) bool {
for _, x := range l {
if x == s {
return true
}
}
return false
}