-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmacros.stencil
102 lines (88 loc) · 4.77 KB
/
macros.stencil
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
{# Uses '-' to trim newlines/whitespace #}
{% macro addMethodParams methodName -%}
{% for struct in types.structs where struct.localName|split:"Request"|join == methodName -%}
{% for method in struct.methods -%}
{% for param in method.parameters -%}
{{ param.name }}: {{ param.typeName|replace:"Swift.","" }}{% if not forloop.last %}, {% endif %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- endmacro %}
{% macro addRequestBody methodName -%}
{% for struct in types.structs where struct.localName|split:"Request"|join == methodName -%}
{% for method in struct.methods -%}
{% for param in method.parameters -%}
{{struct.name}}({{ param.name }}: {{ param.name }}){% if not forloop.last %}, {% endif %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- endmacro %}
{% macro addActivityMethodParams methodName -%}
{% for struct in types.structs where struct.localName|split:"Request"|join == methodName -%}
{% for method in struct.methods -%}
{# First handle non-parameters fields #}
{% for param in method.parameters where param.name != "_type" and param.name != "timestampMs" and param.name != "parameters" -%}
{{ param.name }}: {{ param.typeName|replace:"Swift.","" }}
{%- endfor %}
{# Then handle parameters fields if they exist #}
{% for param in method.parameters where param.name == "parameters" -%}
{% if param.type.variables.count > 0 %}, {% endif %}
{% for var in param.type.variables -%}
{{ var.name }}: {{ var.typeName|replace:"Swift.","" }}{% if not forloop.last %}, {% endif %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- endmacro %}
{% macro getActivityType methodName -%}
{% for struct in types.structs where struct.localName|split:"Request"|join == methodName -%}
{% for method in struct.methods -%}
{% for param in method.parameters where param.name == "_type" -%}
.{{ param.type.cases.0.rawValue }}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- endmacro %}
{% macro getIntentParams intentStructName -%}
{% for struct in types.structs where struct.localName == intentStructName -%}
{% map struct.variables into params -%}{{ maploop.item.name }}: {{ maploop.item.name }}{%- endmap %}
{{ params|join:", " }}
{%- endfor %}
{%- endmacro %}
{% macro generateActivityMethod methodName -%}
{% set bodyInput method.parameters.0.typeName %}
{% set returnType method.returnType.name %}
{# e.g. GetWhoamiRequest #}
{% set requestStruct %}{{methodName}}Request{% endset %}
{% set intentStructName -%}
{% for struct in types.structs where struct.localName == requestStruct -%}
{% for var in struct.variables where var.name == "parameters" -%}
{{ var.typeName|replace:"Components.Schemas.","" }}
{%- endfor %}
{%- endfor %}
{%- endset %}
public func {{ method.callName|lowerFirstLetter }}({% call addActivityMethodParams method.callName %}) async throws -> Operations.{{method.callName}}.Output {
// Create the {{ intentStructName }}
let {{ method.callName|lowerFirstLetter }}Intent = Components.Schemas.{{ intentStructName }}({% call getIntentParams intentStructName %})
// Create the {{ requestStruct }}
let {{ method.callName|lowerFirstLetter }}Request = Components.Schemas.{{ requestStruct }}(
_type: {% call getActivityType method.callName %},
timestampMs: String(Int(Date().timeIntervalSince1970 * 1000)),
{% for struct in types.structs where struct.name|hasSuffix:requestStruct -%}
{% for type in struct.containedTypes where type.parentName|replace:"Components.Schemas.","" == requestStruct and type.localName == "CodingKeys" -%}
{% for case in type.cases where case.name != "_type" and case.name != "timestampMs" and case.name != "parameters" -%}
{{ case.name }}: {{ case.name }},
{%- endfor %}
{%- endfor %}
{%- endfor %}
parameters: {{ method.callName|lowerFirstLetter }}Intent
)
// Create the input for the {{ method.callName }} method
let input = Operations.{{method.callName}}.Input(
headers: .init(accept: [.init(contentType: .json)]),
body: .json({{ requestStruct|lowerFirstLetter }})
)
// Call the {{ method.callName }} method using the underlyingClient
return try await underlyingClient.{{ method.callName }}(input)
}
{%- endmacro %}