summaryrefslogtreecommitdiff
path: root/game/codegen/main.go
blob: 4eff79170792211714c357dbb1f91f383411a1df (plain)
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
// Copyright 2018 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"bufio"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
	"text/template"
)

var utilities = map[string]string {
	"ApiAccount":
`

	var _wallet_dict = null
	var wallet_dict : Dictionary:
		get:
			if _wallet_dict == null:
				if _wallet == null:
					return {}
				var json = JSON.new()
				if json.parse(_wallet) != OK:
					return {}
				_wallet_dict = json.get_data()
			return _wallet_dict as Dictionary
`,
}

const codeTemplate string = `### Code generated by codegen/main.go. DO NOT EDIT. ###

extends RefCounted
class_name NakamaAPI
{{- range $defname, $definition := .Definitions }}
{{- $classname := $defname | title }}
{{- if isRefToEnum $classname }}

# {{ enumSummary $definition | stripNewlines }}
{{- range $idx, $val := ($definition | enumDescriptions) }}
# {{ $val }}
{{- end -}}
# {{ $definition | enumDescriptions }}
enum {{ $classname | title }} { {{- range $idx, $enum := $definition.Enum }}{{ $enum }} = {{ $idx }},{{- end -}} }
{{- else }}

# {{ $definition.Description | stripNewlines }}
class {{ $classname }} extends NakamaAsyncResult:

	const _SCHEMA = {
		{{- range $propname, $property := $definition.Properties }}
		{{- $fieldname := $propname | pascalToSnake }}
		{{- $_field := printf "_%s" $fieldname }}
		{{- $gdType := godotType $property.Type $property.Ref $property.Items.Type $property.Items.Ref (isRefToEnum (cleanRef $property.Ref)) }}
		"{{ $fieldname }}": {"name": "{{ $_field }}", "type": {{ $gdType | godotSchemaType }}, "required": false
		{{- if eq $property.Type "array" -}}
			, "content": {{ (godotType $property.Items.Type $property.Items.Ref "" "" false) | godotSchemaType }}
		{{- else if eq $property.Type "object" -}}
			, "content": {{ (godotType $property.AdditionalProperties.Type "" "" "" false) | godotSchemaType  }}
		{{- end -}}
		},
		{{- end }}
	}

        {{- range $propname, $property := $definition.Properties }}
        {{- $fieldname := $propname | pascalToSnake }}
        {{- $_field := printf "_%s" $fieldname }}
        {{- $gdType := godotType $property.Type $property.Ref $property.Items.Type $property.Items.Ref (isRefToEnum (cleanRef $property.Ref)) }}
	{{- $gdDef := $gdType | godotDef }}

	# {{ $property.Description }}
	var {{ $_field }}
	var {{ $fieldname }} : {{ $gdType }}:
		get:
		{{- if $property.Ref }}
			{{- if isRefToEnum (cleanRef $property.Ref) }}{{/* Enums */}}
			return {{ cleanRef $property.Ref }}.values()[0] if not {{ cleanRef $property.Ref }}.values().has({{ $_field }}) else {{ $_field }}
			{{- else }}{{/* Object reference */}}
			return _{{ $fieldname }} as {{ $gdType }}
			{{- end }}
		{{- else if eq $property.Type "object"}}{{/* Dictionaries */}}
			return Dictionary() if not {{ $_field }} is Dictionary else {{ $_field }}.duplicate()
		{{- else }}{{/* Simple type */}}
			return {{ $gdDef }} if not {{ $_field }} is {{ $gdType }} else {{ $gdType }}({{ $_field }})
		{{- end }}
		{{- end }}

	{{- godotClassUtils $classname }}

	func _init(p_exception = null):
		super(p_exception)

	static func create(p_ns : GDScript, p_dict : Dictionary) -> {{ $classname }}:
		return _safe_ret(NakamaSerializer.deserialize(p_ns, "{{ $classname }}", p_dict), {{ $classname }}) as {{ $classname }}

	func serialize() -> Dictionary:
		return NakamaSerializer.serialize(self)

	func _to_string() -> String:
		if is_exception():
			return get_exception()._to_string()
		var output : String = ""
            {{- range $propname, $property := $definition.Properties }}
            {{- $fieldname := $propname | pascalToSnake }}
            {{- $_field := printf "_%s" $fieldname }}
            {{- if eq $property.Type "array" }}
		output += "{{ $fieldname }}: %s, " % [{{ $_field }}]
            {{- else if eq $property.Type "object" }}
		var map_string : String = ""
		if typeof({{ $_field }}) == TYPE_DICTIONARY:
			for k in {{ $_field }}:
				map_string += "{%s=%s}, " % [k, {{ $_field }}[k]]
		output += "{{ $fieldname }}: [%s], " % map_string
            {{- else }}
		output += "{{ $fieldname }}: %s, " % {{ $_field }}
            {{- end }}
            {{- end }}
		return output
    {{- end }}
{{- end }}

# The low level client for the Nakama API.
class ApiClient extends RefCounted:

	var _base_uri : String

	var _http_adapter
	var _namespace : GDScript
	var _server_key : String
	var auto_refresh := true
	var auto_refresh_time := 300

	var auto_retry : bool:
		set(p_value):
			_http_adapter.auto_retry = p_value
		get:
			return _http_adapter.auto_retry

	var auto_retry_count : int:
		set(p_value):
			_http_adapter.auto_retry_count = p_value
		get:
			return _http_adapter.auto_retry_count

	var auto_retry_backoff_base : int:
		set(p_value):
			_http_adapter.auto_retry_backoff_base = p_value
		get:
			return _http_adapter.auto_retry_backoff_base

	var last_cancel_token:
		get:
			return _http_adapter.get_last_token()

	func _init(p_base_uri : String, p_http_adapter, p_namespace : GDScript, p_server_key : String, p_timeout : int = 10):
		_base_uri = p_base_uri
		_http_adapter = p_http_adapter
		_http_adapter.timeout = p_timeout
		_namespace = p_namespace
		_server_key = p_server_key

	func _refresh_session(p_session : NakamaSession):
		if auto_refresh and p_session.is_valid() and p_session.refresh_token and not p_session.is_refresh_expired() and p_session.would_expire_in(auto_refresh_time):
			var request = ApiSessionRefreshRequest.new()
			request._token = p_session.refresh_token
			return await session_refresh_async(_server_key, "", request)
		return null

	func cancel_request(p_token):
		if p_token:
			_http_adapter.cancel_request(p_token)

        {{- range $url, $path := .Paths }}
        {{- range $method, $operation := $path}}

	# {{ $operation.Summary | stripNewlines }}
        {{- if $operation.Responses.Ok.Schema.Ref }}
	func {{ $operation.OperationId | apiFuncName }}_async(
        {{- else }}
	func {{ $operation.OperationId | apiFuncName }}_async(
        {{- end}}

        {{- if $operation.Security }}
        {{- with (index $operation.Security 0) }}
            {{- range $key, $value := . }}
                {{- if eq $key "BasicAuth" }}
		p_basic_auth_username : String
		, p_basic_auth_password : String
                {{- else if eq $key "HttpKeyAuth" }}
		p_bearer_token : String
                {{- end }}
            {{- end }}
        {{- end }}
        {{- else }}
		p_session : NakamaSession
        {{- end }}

        {{- range $parameter := $operation.Parameters }}
        {{- $argument := $parameter.Name | prependParameter }}
	{{- if not $parameter.Required }}{{/* Godot does not support typed optional parameters yet. */}}
		, {{ $argument }} = null # : {{ $parameter.Type }}
        {{- else if eq $parameter.In "body" }}
            {{- if eq $parameter.Schema.Type "string" }}
		, {{ $argument }} : String
            {{- else }}
		, {{ $argument }} : {{ $parameter.Schema.Ref | cleanRef }}
            {{- end }}
        {{- else }}
		, {{ $argument }} : {{ godotType $parameter.Type $parameter.Schema.Ref $parameter.Items.Type "" (isRefToEnum (cleanRef $parameter.Schema.Ref)) }}
        {{- end }}
	{{- end }}
	)
	{{- if $operation.Responses.Ok.Schema.Ref }} -> {{ $operation.Responses.Ok.Schema.Ref | cleanRef }}
	{{- else }} -> NakamaAsyncResult
	{{- end }}:
        {{- $classname := "NakamaAsyncResult" }}
        {{- if $operation.Responses.Ok.Schema.Ref }}
          {{- $classname = $operation.Responses.Ok.Schema.Ref | cleanRef }}
        {{- end }}
        {{- if not $operation.Security }}
		var try_refresh = await _refresh_session(p_session)
		if try_refresh != null:
			if try_refresh.is_exception():
				return {{ $classname }}.new(try_refresh.get_exception())
			await p_session.refresh(try_refresh)
        {{- end }}
		var urlpath : String = "{{- $url }}"
            {{- range $parameter := $operation.Parameters }}
            {{- $argument := $parameter.Name | prependParameter }}
            {{- if eq $parameter.In "path" }}
		urlpath = urlpath.replace("{{- print "{" $parameter.Name "}"}}", NakamaSerializer.escape_http({{ $argument }}))
            {{- end }}
            {{- end }}
		var query_params = ""
            {{- range $parameter := $operation.Parameters }}
            {{- $argument := $parameter.Name | prependParameter }}
            {{- $snakecase := $parameter.Name | pascalToSnake }}
            {{- if eq $parameter.In "query"}}
            {{- if $parameter.Required }}
		if true: # Hack for static checks
            {{- else }}
		if {{ $argument }} != null:
            {{- end }}
                {{- if eq $parameter.Type "integer" }}
			query_params += "{{- $snakecase }}=%d&" % {{ $argument }}
                {{- else if eq $parameter.Type "string" }}
			query_params += "{{- $snakecase }}=%s&" % NakamaSerializer.escape_http({{ $argument }})
                {{- else if eq $parameter.Type "boolean" }}
			query_params += "{{- $snakecase }}=%s&" % str(bool({{ $argument }})).to_lower()
                {{- else if eq $parameter.Type "array" }}
			for elem in {{ $argument }}:
				query_params += "{{- $snakecase }}=%s&" % elem
                {{- else }}
		{{ $parameter }} // ERROR
                {{- end }}
            {{- end }}
            {{- end }}
		var uri = "%s%s%s" % [_base_uri, urlpath, "?" + query_params if query_params else ""]
		var method = "{{- $method | uppercase }}"
		var headers = {}
            {{- if $operation.Security }}
            {{- with (index $operation.Security 0) }}
                {{- range $key, $value := . }}
                    {{- if eq $key "BasicAuth" }}
		var credentials = Marshalls.utf8_to_base64(p_basic_auth_username + ":" + p_basic_auth_password)
		var header = "Basic %s" % credentials
		headers["Authorization"] = header
                    {{- else if eq $key "HttpKeyAuth" }}
		if (p_bearer_token):
			var header = "Bearer %s" % p_bearer_token
			headers["Authorization"] = header
                    {{- end }}
                {{- end }}
            {{- end }}
            {{- else }}
		var header = "Bearer %s" % p_session.token
		headers["Authorization"] = header
            {{- end }}

		var content : PackedByteArray
            {{- range $parameter := $operation.Parameters }}
            {{- $argument := $parameter.Name | prependParameter }}
            {{- if eq $parameter.In "body" }}
                {{- if eq $parameter.Schema.Type "string" }}
		content = JSON.stringify({{ $argument }}).to_utf8_buffer()
                {{- else }}
		content = JSON.stringify({{ $argument }}.serialize()).to_utf8_buffer()
                {{- end }}
            {{- end }}
            {{- end }}

		var result = await _http_adapter.send_async(method, uri, headers, content)
		if result is NakamaException:
			return {{ $classname }}.new(result)

            {{- if $operation.Responses.Ok.Schema.Ref }}
		var out : {{ $classname }} = NakamaSerializer.deserialize(_namespace, "{{ $classname }}", result)
		return out
            {{- else }}
		return NakamaAsyncResult.new()
            {{- end}}
{{- end }}
{{- end }}
`

func convertRefToClassName(input string) (className string) {
	cleanRef := strings.TrimPrefix(input, "#/definitions/")
	className = strings.Title(cleanRef)
	return
}

func stripNewlines(input string) (output string) {
	output = strings.Replace(input, "\n", " ", -1)
	return
}

func prependParameter(input string) (output string) {
	output = "p_" + pascalToSnake(input)
	return
}

func pascalToSnake(input string) (output string) {
	output = ""
	prev_low := false
	for _, v := range input {
		is_cap := v >= 'A' && v <= 'Z'
		is_low := v >= 'a' && v <= 'z'
		if is_cap && prev_low {
			output = output + "_"
		}
		output += strings.ToLower(string(v))
		prev_low = is_low
	}
	return
}

func apiFuncName(input string) (output string) {
	output = pascalToSnake(input[7:])
	return
}

func godotType(p_type string, p_ref string, p_item_type string, p_extra string, p_is_enum bool) (out string) {

	is_array := false
	is_dict := false
	switch p_type {
		case "integer":
			out = "int"
		case "string":
			out = "String"
		case "boolean":
			out = "bool"
		case "array":
			is_array = true
		case "object":
			is_dict = true
		default:
			if p_is_enum {
				out = "int"
			} else {
				out = convertRefToClassName(p_ref)
			}
	}

	if is_array {
		switch p_item_type {
			case "integer":
				out = "PackedIntArray"
				return
			case "string":
				out = "PackedStringArray"
				return
			case "boolean":
				out = "PackedIntArray"
				return
			default:
				out = "Array"
		}
	}
	if is_dict {
		out = "Dictionary"
	}
	return
}

func godotDef(p_type string) (out string) {
	switch(p_type) {
		case "bool": out = "false"
		case "int": out = "0"
		case "String": out = "\"\""
		case "PackedIntArray": out = "PackedIntArray()"
		case "PackedStringArray": out = "PackedStringArray()"
		case "Array": out = "Array()"
		case "Dictionary": out = "Dictionary()"
	}
	return
}

func godotLooseType(p_type string) (out string) {
	switch(p_type) {
		case "PackedStringArray", "PackedIntArray":
			out = "Array"
		default:
			out = p_type
	}
	return
}

func godotSchemaType(p_type string) (out string) {
	out = "TYPE_"
	switch(p_type) {
		case "bool": out += "BOOL"
		case "int": out += "INT"
		case "String": out += "STRING"
		case "PackedIntArray": out += "ARRAY"
		case "PackedStringArray": out += "ARRAY"
		case "Array": out += "ARRAY"
		case "Dictionary": out += "DICTIONARY"
		default: out = "\"" + p_type + "\""
	}
	return
}

func pascalToCamel(input string) (camelCase string) {
	if input == "" {
		return ""
	}

	camelCase = strings.ToLower(string(input[0]))
	camelCase += string(input[1:])
	return camelCase
}

func camelToPascal(camelCase string) (pascalCase string) {

	if len(camelCase) <= 0 {
		return ""
	}

	pascalCase = strings.ToUpper(string(camelCase[0])) + camelCase[1:]
	return
}

func enumSummary(def Definition) string {
	// quirk of swagger generation: if enum doesn't have a title
	// then the title can be found as the first entry in the split description.
	if def.Title != "" {
		return def.Title
	}

	split := strings.Split(def.Description, "\n")

	if len(split) <= 0 {
		panic("No newlines in enum description found.")
	}

	return split[0]
}

func enumDescriptions(def Definition) (output []string) {

	split := strings.Split(def.Description, "\n")

	if len(split) <= 0 {
		panic("No newlines in enum description found.")
	}

	if def.Title != "" {
		return split
	}

	// quirk of swagger generation: if enum doesn't have a title
	// then the title can be found as the first entry in the split description.
	// so ignore for individual enum descriptions.
	return split[2:]
}

type Definition struct {
	Properties map[string]struct {
		Type  string
		Ref   string   `json:"$ref"` // used with object
		Items struct { // used with type "array"
			Type string
			Ref  string `json:"$ref"`
		}
		AdditionalProperties struct {
			Type string // used with type "map"
		}
		Format      string // used with type "boolean"
		Description string
	}
	Enum        []string
	Description string
	// used only by enums
	Title string
}

func godotClassUtils(p_name string) string {
	if val, ok := utilities[p_name]; ok {
		return val
	}
	return ""
}

func main() {
	// Argument flags
	var output = flag.String("output", "", "The output for generated code.")
	flag.Parse()

	inputs := flag.Args()
	if len(inputs) < 1 {
		fmt.Printf("No input file found: %s\n\n", inputs)
		fmt.Println("openapi-gen [flags] inputs...")
		flag.PrintDefaults()
		return
	}

	input := inputs[0]
	content, err := ioutil.ReadFile(input)
	if err != nil {
		fmt.Printf("Unable to read file: %s\n", err)
		return
	}

	var schema struct {
		Paths map[string]map[string]struct {
			Summary     string
			OperationId string
			Responses   struct {
				Ok struct {
					Schema struct {
						Ref string `json:"$ref"`
					}
				} `json:"200"`
			}
			Parameters []struct {
				Name     string
				In       string
				Required bool
				Type     string   // used with primitives
				Items    struct { // used with type "array"
					Type string
				}
				Schema struct { // used with http body
					Type string
					Ref  string `json:"$ref"`
				}
                Format   string // used with type "boolean"
			}
			Security []map[string][]struct {
			}
		}
		Definitions map[string]Definition
	}

	if err := json.Unmarshal(content, &schema); err != nil {
		fmt.Printf("Unable to decode input %s : %s\n", input, err)
		return
	}

	fmap := template.FuncMap{
		"cleanRef": convertRefToClassName,
		"stripNewlines": stripNewlines,
		"title": strings.Title,
		"uppercase": strings.ToUpper,
		"prependParameter": prependParameter,
		"pascalToSnake": pascalToSnake,
		"apiFuncName": apiFuncName,
		"godotType": godotType,
		"godotLooseType": godotLooseType,
		"godotSchemaType": godotSchemaType,
		"godotDef": godotDef,
		"isRefToEnum": func(ref string) bool {
			if len(ref) == 0 {
				return false
			}
			// swagger schema definition keys have inconsistent casing
			var camelOk bool
			var pascalOk bool
			var enums []string

			asCamel := pascalToCamel(ref)
			if _, camelOk = schema.Definitions[asCamel]; camelOk {
				enums = schema.Definitions[asCamel].Enum
			}

			asPascal := camelToPascal(ref)
			if _, pascalOk = schema.Definitions[asPascal]; pascalOk {
				enums = schema.Definitions[asPascal].Enum
			}

			if !pascalOk && !camelOk {
				fmt.Printf("no definition found: %v", ref)
				return false
			}

			return len(enums) > 0
		},
		"enumDescriptions": enumDescriptions,
		"enumSummary":      enumSummary,
		"godotClassUtils": godotClassUtils,
	}
	tmpl, err := template.New(input).Funcs(fmap).Parse(codeTemplate)
	if err != nil {
		fmt.Printf("Template parse error: %s\n", err)
		return
	}

	if len(*output) < 1 {
		tmpl.Execute(os.Stdout, schema)
		return
	}

	f, err := os.Create(*output)
	if err != nil {
		fmt.Printf("Unable to create file: %s\n", err)
		return
	}
	defer f.Close()

	writer := bufio.NewWriter(f)
	tmpl.Execute(writer, schema)
	writer.Flush()
}