-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverHelpers.go.tmpl
55 lines (43 loc) · 1.33 KB
/
serverHelpers.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
{{- define "server_helpers" -}}
{{- $messages := .Types -}}
{{- $typeMap := .TypeMap -}}
{{- range $messages }}
def validate_{{ .Name }}(value):
{{- stderrPrintf "OPTS: %q" .Fields -}}
{{- range .Fields }}
{{- if .Optional }}
if "{{ template "fieldName" dict "Field" . }}" in value and not validate_type(value["{{ template "fieldName" dict "Field" . }}"], "{{ template "pyType" dict "Type" .Type "TypeMap" $typeMap }}"):
return False
{{- else }}
if "{{ template "fieldName" dict "Field" . }}" not in value or not validate_type(value["{{ template "fieldName" dict "Field" . }}"], "{{ template "pyType" dict "Type" .Type "TypeMap" $typeMap }}"):
return False
{{- end }}
{{- end }}
return True
{{- end }}
TYPE_VALIDATORS: Dict[str, Callable[[any], bool]] = {}
TYPE_VALIDATORS = {
{{ range $messages }}
{{ .Name }}: validate{{ .Name }},
{{ end }}
}
PY_TYPES = [
"bool",
"int",
"float",
"str",
"list",
"tuple",
"dict",
"NoneType"
]
def validate_type(value, type):
if type in PY_TYPES:
return isinstance(value, type)
if type == "function":
return callable(value)
validator = TYPE_VALIDATORS.get(type)
if not validator:
return False
return validator(value)
{{- end -}}