-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go.tmpl
145 lines (121 loc) · 4.19 KB
/
main.go.tmpl
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
{{- define "main" -}}
{{- /* Options with default values. */ -}}
{{- $opts := dict -}}
{{- set $opts "client" (ternary (in .Opts.client "" "true") true false) -}}
{{- set $opts "server" (ternary (in .Opts.server "" "true") true false) -}}
{{- set $opts "webrpcHeader" (ternary (eq (default .Opts.webrpcHeader "true") "false") false true) -}}
{{- /* Print help on -help. */ -}}
{{- if exists .Opts "help" -}}
{{- template "help" $opts -}}
{{- exit 0 -}}
{{- end -}}
{{- /* Print help on unsupported option. */ -}}
{{- range $k, $v := .Opts }}
{{- if not (exists $opts $k) -}}
{{- stderrPrintf "-%v=%q is not supported target option\n\nUsage:\n" $k $v -}}
{{- template "help" $opts -}}
{{- exit 1 -}}
{{- end -}}
{{- end -}}
{{- if ne .WebrpcVersion "v1" -}}
{{- stderrPrintf "%s generator error: unsupported webrpc version %s\n" .WebrpcTarget .WebrpcVersion -}}
{{- exit 1 -}}
{{- end -}}
{{- if not (minVersion .WebrpcGenVersion "v0.7.0") -}}
{{- stderrPrintf "%s generator error: unsupported webrpc-gen version %s, please update\n" .WebrpcTarget .WebrpcGenVersion -}}
{{- exit 1 -}}
{{- end -}}
{{- set $opts "" false -}}
{{- range $_, $service := .Services -}}
{{- range $_, $method := $service.Methods -}}
{{ if eq $method.StreamOutput true -}}
{{- set $opts "streaming" true -}}
{{- end -}}
{{- end -}}
{{- end }}
{{- /* Map webrpc core types to JS. */ -}}
{{- $typeMap := dict }}
{{- set $typeMap "null" "null" -}}
{{- set $typeMap "any" "any" -}}
{{- set $typeMap "byte" "string" -}}
{{- set $typeMap "bool" "boolean" -}}
{{- set $typeMap "uint" "number" -}}
{{- set $typeMap "uint8" "number" -}}
{{- set $typeMap "uint16" "number" -}}
{{- set $typeMap "uint32" "number" -}}
{{- set $typeMap "uint64" "number" -}}
{{- set $typeMap "int" "number" -}}
{{- set $typeMap "int8" "number" -}}
{{- set $typeMap "int16" "number" -}}
{{- set $typeMap "int32" "number" -}}
{{- set $typeMap "int64" "number" -}}
{{- set $typeMap "float32" "number" -}}
{{- set $typeMap "float64" "number" -}}
{{- set $typeMap "string" "string" -}}
{{- set $typeMap "timestamp" "string" -}}
/* eslint-disable */
// {{.SchemaName}} {{.SchemaVersion}} {{.SchemaHash}}
// --
// Code generated by webrpc-gen@{{.WebrpcGenVersion}} with {{.WebrpcTarget}} generator. DO NOT EDIT.
//
// {{.WebrpcGenCommand}}
export const WebrpcHeader = "Webrpc"
export const WebrpcHeaderValue = "{{ .WebrpcHeader }}"
// WebRPC description and code-gen version
export const WebRPCVersion = "{{.WebrpcVersion}}"
// Schema version of your RIDL schema
export const WebRPCSchemaVersion = "{{.SchemaVersion}}"
// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "{{.SchemaHash}}"
type WebrpcGenVersions = {
webrpcGenVersion: string;
codeGenName: string;
codeGenVersion: string;
schemaName: string;
schemaVersion: string;
};
export function VersionFromHeader(headers: Headers): WebrpcGenVersions {
const headerValue = headers.get(WebrpcHeader);
if (!headerValue) {
return {
webrpcGenVersion: "",
codeGenName: "",
codeGenVersion: "",
schemaName: "",
schemaVersion: "",
};
}
return parseWebrpcGenVersions(headerValue);
}
function parseWebrpcGenVersions(header: string): WebrpcGenVersions {
const versions = header.split(";");
if (versions.length < 3) {
return {
webrpcGenVersion: "",
codeGenName: "",
codeGenVersion: "",
schemaName: "",
schemaVersion: "",
};
}
const [_, webrpcGenVersion] = versions[0].split("@");
const [codeGenName, codeGenVersion] = versions[1].split("@");
const [schemaName, schemaVersion] = versions[2].split("@");
return {
webrpcGenVersion,
codeGenName,
codeGenVersion,
schemaName,
schemaVersion,
};
}
{{template "types" dict "Types" .Types "Services" .Services "TypeMap" $typeMap}}
{{- if $opts.client}}
{{template "client" dict "Services" .Services "Opts" $opts "TypeMap" $typeMap}}
{{template "clientHelpers" dict "WebrpcErrors" .WebrpcErrors "SchemaErrors" .Errors "Opts" $opts }}
{{- end}}
{{- if $opts.server}}
{{template "server" dict "Services" .Services "Opts" $opts "TypeMap" $typeMap}}
{{template "serverHelpers" dict "Types" .Types "TypeMap" $typeMap}}
{{- end}}
{{- end}}