-
Notifications
You must be signed in to change notification settings - Fork 389
/
data-io-template.go.ftlh
304 lines (285 loc) · 17 KB
/
data-io-template.go.ftlh
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
<#--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
-->
<#-- Prevent freemarker from escaping stuff -->
<#outputformat "undefined">
<#-- Declare the name and type of variables passed in to the template -->
<#-- @ftlvariable name="languageName" type="java.lang.String" -->
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.go.GoLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.DataIoTypeDefinition" -->
${helper.fileName(protocolName, languageName, outputFlavor)?replace(".", "/")}/model/${type.name}.go
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 model
<#macro importSectionWithContentBelow>
<#local sectionContent><#nested></#local>
import (
<#if helper.getRequiredImportsForDataIo()?has_content>
<#list helper.getRequiredImportsForDataIo() as import>
${import}
</#list>
</#if>
)
${sectionContent}
</#macro>
<#macro emitImport import>${helper.emitDataIoRequiredImport(import)}</#macro>
<#macro emitImportWithAlias alias import>${helper.emitDataIoRequiredImport(alias, import)}</#macro>
<@importSectionWithContentBelow><@emitImport import="github.com/apache/plc4x/plc4go/internal/plc4go/spi/utils" /><@emitImport import="github.com/apache/plc4x/plc4go/internal/plc4go/spi/values" /><@emitImportWithAlias alias="api" import="github.com/apache/plc4x/plc4go/pkg/plc4go/values" />
// Code generated by code-generation. DO NOT EDIT.
<#-- TODO: the code below implies that parserArguments will be null if not present... not pretty -->
<#if type.parserArguments.isPresent()><#assign parserArguments=type.parserArguments.orElseThrow()></#if>
func ${type.name}Parse(readBuffer utils.ReadBuffer<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name} <#if helper.isComplexTypeReference(parserArgument.type) && !helper.isEnumTypeReference(parserArgument.type)>I</#if>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#sep>, </#sep></#list></#if>) (api.PlcValue, error) {
readBuffer.PullContext("${type.name}")
switch {
<#list type.switchField.cases as case>
<@compress single_line=true>
<#if case.discriminatorValueTerms?has_content>
case
<#list case.discriminatorValueTerms as discriminatorValueTerm>
${helper.toParseExpression(null, null, type.switchField.discriminatorExpressions[discriminatorValueTerm?index], parserArguments)} ==
<#if helper.isComplexTypeReference(parserArguments[discriminatorValueTerm?index].type)>
<#if helper.isEnumTypeReference(parserArguments[discriminatorValueTerm?index].type)>
<#assign enumType=helper.getLanguageTypeNameForTypeReference(parserArguments[discriminatorValueTerm?index].type)>
<#assign enumTypeWithoutTraces=tracer.removeTraces(enumType)>
<#assign enumTypeTraces=tracer.extractTraces(enumType)>
<#assign enumValue=helper.toParseExpression(null, parserArguments[discriminatorValueTerm?index].type, discriminatorValueTerm, parserArguments)>
<#assign enumValueWithoutTraces=tracer.removeTraces(enumValue)>
<#assign enumValueTraces=tracer.extractTraces(enumValue)>
${enumTypeTraces}${enumValueTraces}${enumTypeWithoutTraces}_${enumValueWithoutTraces?replace("Get","")?replace("()","")}
<#else>
${helper.toParseExpression(null, parserArguments[discriminatorValueTerm?index].type, discriminatorValueTerm, parserArguments)}
</#if>
<#else>
${helper.toParseExpression(null, parserArguments[discriminatorValueTerm?index].type, discriminatorValueTerm, parserArguments)}
</#if>
<#sep> && </#sep>
</#list>
<#else>
default
</#if>: // ${case.name}
</@compress>
<#assign valueDefined=false>
<#if case.name == "Struct">
// Struct
_map := map[string]api.PlcValue{}
<#assign valueDefined=true>
</#if>
<#list case.fields as field>
<#switch field.typeName>
<#case "array">
<#assign arrayField = field.asArrayField().orElseThrow()>
// Array Field (${arrayField.name})
var ${arrayField.name} []api.PlcValue
for i := 0; i < int(${helper.toParseExpression(null, null, arrayField.loopExpression, parserArguments)}); i++ {
_item, _itemErr := <#if helper.isSimpleTypeReference(arrayField.type)>${helper.getReadBufferReadMethodCall(arrayField.name, arrayField.type.asSimpleTypeReference().orElseThrow(), arrayField)}<#else>Complex type array in data-io parsing currently not implemented</#if>
if _itemErr != nil {
return nil, errors.Wrap(_itemErr, "Error parsing 'value' field")<@emitImport import="github.com/pkg/errors" />
}
${arrayField.name} = append(${arrayField.name}, ${helper.getPlcValueTypeForTypeReference(arrayField.type)}(_item))
}
<#if arrayField.name == "value">
<#assign valueDefined=true>
</#if>
<#break>
<#case "manual">
<#assign arrayField = field.asManualField().orElseThrow()>
// Manual Field (${arrayField.name})
${arrayField.name}, _${arrayField.name}Err := ${helper.toParseExpression(arrayField, arrayField.type, arrayField.parseExpression, parserArguments)}
if _${arrayField.name}Err != nil {
return nil, errors.Wrap(_${arrayField.name}Err, "Error parsing '${arrayField.name}' field")<@emitImport import="github.com/pkg/errors" />
}
<#if arrayField.name == "value">
<#assign valueDefined=true>
</#if>
<#break>
<#case "reserved">
<#assign reservedField = field.asReservedField().orElseThrow()>
// Reserved Field (Just skip the bytes)
if _, _err := ${helper.getReadBufferReadMethodCall("reserved", reservedField.type.asSimpleTypeReference().orElseThrow(), reservedField)}; _err != nil {
return nil, errors.Wrap(_err, "Error parsing reserved field")<@emitImport import="github.com/pkg/errors" />
}
<#break>
<#case "simple">
<#assign simpleField = field.asSimpleField().orElseThrow()>
// Simple Field (${simpleField.name})
<#if case.name == "Struct" || ((case.name == "DATE_AND_TIME") && ((simpleField.name == "year") || (simpleField.name == "month") || (simpleField.name == "day") || (simpleField.name == "hour") || (simpleField.name == "minutes") || (simpleField.name == "seconds") || (simpleField.name == "secondsSinceEpoch"))) || ((case.name == "DATE") && ((simpleField.name == "year") || (simpleField.name == "month") || (simpleField.name == "day"))) || ((case.name == "TIME_OF_DAY") && ((simpleField.name == "hour") || (simpleField.name == "minutes") || (simpleField.name == "seconds"))) || simpleField.name == "value">${simpleField.name}<#else>_</#if>, _${simpleField.name}Err := <#if helper.isSimpleTypeReference(simpleField.type)>${helper.getReadBufferReadMethodCall(simpleField.name, simpleField.type.asSimpleTypeReference().orElseThrow(), simpleField)}<#else>${simpleField.type.asComplexTypeReference().orElseThrow().name}Parse(io<#if simpleField.type.asComplexTypeReference().orElseThrow().params.isPresent()>, <#list simpleField.type.asComplexTypeReference().orElseThrow().params.orElseThrow() as parserArgument>(${helper.getLanguageTypeNameForTypeReference(helper.getArgumentType(simpleField.type, parserArgument?index))}) (${helper.toParseExpression(simpleField, helper.getArgumentType(simpleField.type, parserArgument?index), parserArgument, parserArguments)})<#sep>, </#sep></#list></#if>)</#if>
if _${simpleField.name}Err != nil {
return nil, errors.Wrap(_${simpleField.name}Err, "Error parsing '${simpleField.name}' field")<@emitImport import="github.com/pkg/errors" />
}
<#if case.name == "Struct">
_map["${case.name}"] = ${helper.getPlcValueTypeForTypeReference(simpleField.type)}(${simpleField.name})
</#if>
<#assign valueDefined=true>
<#break>
</#switch>
</#list>
<#if valueDefined>
<#switch case.name>
<#case "TIME_OF_DAY">
<#if helper.hasFieldsWithNames(case.fields, "hour", "minutes", "seconds")>
value := time.Date(0,0,0, int(hour), int(minutes), int(seconds), 0, nil)<@emitImport import="time" />
</#if>
readBuffer.CloseContext("${type.name}")
return values.NewPlcTIME_OF_DAY(value), nil
<#break>
<#case "DATE">
<#if helper.hasFieldsWithNames(case.fields, "year", "month", "day")>
value := time.Date(int(year), time.Month(month), int(day), 0, 0, 0, 0, nil)<@emitImport import="time" />
</#if>
readBuffer.CloseContext("${type.name}")
return values.NewPlcDATE(value), nil
<#break>
<#case "DATE_AND_TIME">
<#if helper.hasFieldsWithNames(case.fields, "year", "month", "day", "hour", "minutes", "seconds")>
value := time.Date(int(year), time.Month(month), int(day), int(hour), int(minutes), int(seconds), 0, nil)<@emitImport import="time" />
<#elseif helper.hasFieldsWithNames(case.fields, "secondsSinceEpoch")>
value := time.Unix(int64(secondsSinceEpoch), 0)<@emitImport import="time" />
</#if>
readBuffer.CloseContext("${type.name}")
return values.NewPlcDATE_AND_TIME(value), nil
<#break>
readBuffer.CloseContext("${type.name}")
<#case "Struct">
readBuffer.CloseContext("${type.name}")
return values.NewPlcStruct(_map), nil
<#break>
<#case "List">
readBuffer.CloseContext("${type.name}")
return values.NewPlcList(value), nil
<#break>
<#default>
readBuffer.CloseContext("${type.name}")
return values.NewPlc${case.name}(value), nil
</#switch>
</#if>
</#list>
}
// TODO: add more info which type it is actually
return nil, errors.New("unsupported type")<@emitImport import="github.com/pkg/errors" />
}
func ${type.name}Serialize(writeBuffer utils.WriteBuffer, value api.PlcValue<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name} <#if helper.isComplexTypeReference(parserArgument.type) && !helper.isEnumTypeReference(parserArgument.type)>I</#if>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#sep>, </#sep></#list></#if>) error {
<#if parserArguments?has_content>
m := struct {
<#list parserArguments as parserArgument>
${parserArgument.name?cap_first} <#if helper.isComplexTypeReference(parserArgument.type) && !helper.isEnumTypeReference(parserArgument.type)>I</#if>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}
</#list>
}{
<#list parserArguments as parserArgument>
${parserArgument.name?cap_first}: ${parserArgument.name},
</#list>
}
_ = m
</#if>
writeBuffer.PushContext("${type.name}")
switch {
<#assign defaultSet=false>
<#list type.switchField.cases as case>
<@compress single_line=true>
<#if case.discriminatorValueTerms?has_content>
case
<#list case.discriminatorValueTerms as discriminatorValueTerm>
${helper.toParseExpression(null, null, type.switchField.discriminatorExpressions[discriminatorValueTerm?index], parserArguments)} ==
<#if helper.isComplexTypeReference(parserArguments[discriminatorValueTerm?index].type)>
<#if helper.isEnumTypeReference(parserArguments[discriminatorValueTerm?index].type)>
<#assign enumType=helper.getLanguageTypeNameForTypeReference(parserArguments[discriminatorValueTerm?index].type)>
<#assign enumTypeWithoutTraces=tracer.removeTraces(enumType)>
<#assign enumTypeTraces=tracer.extractTraces(enumType)>
<#assign enumValue=helper.toParseExpression(null, parserArguments[discriminatorValueTerm?index].type, discriminatorValueTerm, parserArguments)>
<#assign enumValueWithoutTraces=tracer.removeTraces(enumValue)>
<#assign enumValueTraces=tracer.extractTraces(enumValue)>
${enumTypeTraces}${enumValueTraces}${enumTypeWithoutTraces}_${enumValueWithoutTraces?replace("Get","")?replace("()","")}
<#else>
${helper.toParseExpression(null, parserArguments[discriminatorValueTerm?index].type, discriminatorValueTerm, parserArguments)}
</#if>
<#else>
${helper.toParseExpression(null, parserArguments[discriminatorValueTerm?index].type, discriminatorValueTerm, parserArguments)}
</#if>
<#sep> && </#sep>
</#list>
<#else>
default<#assign defaultSet=true>
</#if>: // ${case.name}
</@compress>
<#list case.fields as field>
<#switch field.typeName>
<#case "array">
<#assign arrayField = field.asArrayField().orElseThrow()>
// Array Field (${arrayField.name})
for i := uint32(0); i < uint32(${helper.toSerializationExpression(null, null, arrayField.loopExpression, parserArguments)}); i++ {
<#if case.name = "Struct">
${arrayField.name} := value.GetValue("${arrayField.name}")
</#if>
_itemErr := <#if helper.isSimpleTypeReference(arrayField.type)>${helper.getWriteBufferWriteMethodCall("", arrayField.type.asSimpleTypeReference().orElseThrow(), arrayField.name + ".GetIndex(i).Get" + helper.getLanguageTypeNameForTypeReference(arrayField.type)?cap_first + "()", arrayField)}<#else>Complex type array in data-io serialization currently not implemented</#if>
if _itemErr != nil {
return errors.Wrap(_itemErr, "Error serializing 'value' field")<@emitImport import="github.com/pkg/errors" />
}
}
<#break>
<#case "manual">
<#assign manualField = field.asManualField().orElseThrow()>
// Manual Field (${manualField.name})
_${manualField.name}Err := ${helper.toSerializationExpression(manualField, manualField.type, manualField.serializeExpression, parserArguments)}
if _${manualField.name}Err != nil {
return errors.Wrap(_${manualField.name}Err, "Error serializing '${manualField.name}' field")<@emitImport import="github.com/pkg/errors" />
}
<#break>
<#case "reserved">
<#assign reservedField = field.asReservedField().orElseThrow()>
// Reserved Field (Just skip the bytes)
if _err := ${helper.getWriteBufferWriteMethodCall("reserved", reservedField.type.asSimpleTypeReference().orElseThrow(), helper.getReservedValue(reservedField), reservedField)}; _err != nil {
return errors.Wrap(_err, "Error serializing reserved field")<@emitImport import="github.com/pkg/errors" />
}
<#break>
<#case "simple">
<#assign simpleField = field.asSimpleField().orElseThrow()>
// Simple Field (${simpleField.name})
if _err := <#if helper.isSimpleTypeReference(simpleField.type)>${helper.getWriteBufferWriteMethodCall(simpleField.name, simpleField.type.asSimpleTypeReference().orElseThrow(), "value.Get" + helper.getLanguageTypeNameForTypeReference(simpleField.type)?cap_first + "()", simpleField)}<#else>${simpleField.type.asComplexTypeReference().orElseThrow().name}Serialize(io, <#if simpleField.type.asComplexTypeReference().orElseThrow().params.isPresent()>, <#list simpleField.type.asComplexTypeReference().orElseThrow().params.orElseThrow() as parserArgument>(${helper.getLanguageTypeNameForTypeReference(helper.getArgumentType(simpleField.type, parserArgument?index))}) (${helper.toParseExpression(simpleField, helper.getArgumentType(simpleField.type, parserArgument?index), parserArgument, parserArguments)})<#sep>, </#sep></#list></#if>)</#if>; _err != nil {
return errors.Wrap(_err, "Error serializing '${simpleField.name}' field")<@emitImport import="github.com/pkg/errors" />
}
<#break>
</#switch>
</#list>
</#list>
<#if !defaultSet>
default:
// TODO: add more info which type it is actually
return errors.New("unsupported type")<@emitImport import="github.com/pkg/errors" />
</#if>
}
writeBuffer.PopContext("${type.name}")
return nil
}
</@importSectionWithContentBelow>
</#outputformat>