forked from apache/plc4x
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata-io-template.java.ftlh
executable file
·562 lines (539 loc) · 38.2 KB
/
data-io-template.java.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
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
<#--
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
https://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.java.JavaLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.ComplexTypeDefinition" -->
${helper.packageName(protocolName, languageName, outputFlavor)?replace(".", "/")}/${type.name}.java
/*
* 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
*
* https://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 ${helper.packageName(protocolName, languageName, outputFlavor)};
import static org.apache.plc4x.java.spi.generation.StaticHelper.*;
import org.apache.plc4x.java.api.model.PlcTag;
import org.apache.plc4x.java.api.value.*;
import org.apache.plc4x.java.spi.generation.EvaluationHelper;
import org.apache.plc4x.java.spi.generation.ParseException;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.ReadBuffer;
import org.apache.plc4x.java.spi.generation.WriteBuffer;
import org.apache.plc4x.java.spi.generation.ByteOrder;
import ${helper.packageName(protocolName, languageName, outputFlavor)}.*;
import org.apache.plc4x.java.spi.values.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.time.*;
import java.util.*;
import java.util.function.Supplier;
// 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>
public class ${type.name} {
private static final Logger LOGGER = LoggerFactory.getLogger(${type.name}.class);
public static PlcValue staticParse(ReadBuffer readBuffer<#if type.parserArguments.isPresent()>, <#list type.parserArguments.orElseThrow() as parserArgument>${helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)} ${parserArgument.name}<#sep>, </#sep></#list></#if>) throws ParseException {
<#assign defaultCaseOutput=false>
<#assign dataIoTypeDefinition=type.asDataIoTypeDefinition().orElseThrow()>
<#list dataIoTypeDefinition.switchField.orElseThrow().cases as case>
<@compress single_line=true>
<#if case.discriminatorValueTerms?has_content>
if(
<#list case.discriminatorValueTerms as discriminatorValueTerm>
<#assign discriminatorExpression=dataIoTypeDefinition.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index].asLiteral().orElseThrow().asVariableLiteral().orElseThrow()>
<#assign discriminatorType=helper.getDiscriminatorTypes()[discriminatorExpression.name]>
EvaluationHelper.equals(
${helper.toParseExpression(dataIoTypeDefinition.switchField.orElseThrow(), discriminatorType, discriminatorExpression, parserArguments)},
<#if discriminatorType.isEnumTypeReference()>
${helper.getLanguageTypeNameForTypeReference(discriminatorType)}.${helper.toParseExpression(dataIoTypeDefinition.switchField.orElseThrow(), discriminatorType, discriminatorValueTerm, parserArguments)}
<#else>
${helper.toParseExpression(dataIoTypeDefinition.switchField.orElseThrow(), discriminatorType, discriminatorValueTerm, parserArguments)}
</#if>
)
<#sep> && </#sep>
</#list>
)
<#else>
<#assign defaultCaseOutput=true>
</#if>
</@compress> { // ${case.name}
<#assign valueDefined=false>
<#list case.fields as field>
<#switch field.typeName>
<#case "array">
<#assign arrayField = field.asArrayField().orElseThrow()>
<#assign elementTypeReference=arrayField.type.elementTypeReference>
// Array field (${arrayField.name})
<#-- Only update curPos if the length expression uses it -->
<#if arrayField.loopExpression.contains("curPos")>
curPos = readBuffer.getPos() - startPos;
</#if>
<#-- If this is a count array, we can directly initialize an array with the given size -->
<#if field.isCountArrayField()>
// Count array
if(${helper.toParseExpression(arrayField, helper.intTypeReference, arrayField.loopExpression,parserArguments)} > Integer.MAX_VALUE) {
throw new ParseException("Array count of " + (${helper.toParseExpression(arrayField, helper.intTypeReference, arrayField.loopExpression,parserArguments)}) + " exceeds the maximum allowed count of " + Integer.MAX_VALUE);
}
List<PlcValue> ${arrayField.name};
{
int itemCount = (int) ${helper.toParseExpression(arrayField, helper.intTypeReference, arrayField.loopExpression,parserArguments)};
${arrayField.name} = new LinkedList<>();
for(int curItem = 0; curItem < itemCount; curItem++) {
${arrayField.name}.add(new ${helper.getPlcValueTypeForTypeReference(elementTypeReference)}((${helper.getLanguageTypeNameForTypeReference(elementTypeReference, false)}) <#if elementTypeReference.isSimpleTypeReference()>${helper.getReadBufferReadMethodCall(elementTypeReference.asSimpleTypeReference().orElseThrow(), "", arrayField)})<#else>${elementTypeReference.asComplexTypeReference().orElseThrow().name}IO.staticParse(readBuffer<#if elementTypeReference.params.isPresent()>, <#list elementTypeReference.params.orElseThrow() as parserArgument>(${helper.getLanguageTypeNameForTypeReference(helper.getArgumentType(elementTypeReference, parserArgument?index), true)}) (${helper.toParseExpression(arrayField, elementTypeReference, parserArgument,parserArguments)})<#sep>, </#sep></#list></#if>)</#if>);
}
}
<#-- In all other cases do we have to work with a list, that is later converted to an array -->
<#else>
<#-- For a length array, we read data till the read position of the buffer reaches a given position -->
<#if arrayField.isLengthArrayField()>
// Length array
long _${arrayField.name}Length = ${helper.toParseExpression(arrayField, helper.intTypeReference, arrayField.loopExpression,parserArguments)};
long ${arrayField.name}EndPos = readBuffer.getPos() + _${arrayField.name}Length;
List<PlcValue> value = new LinkedList<>();
while(readBuffer.getPos() < ${arrayField.name}EndPos) {
value.add(
<#if elementTypeReference.isSimpleTypeReference()>
new ${helper.getPlcValueTypeForTypeReference(elementTypeReference)}(${helper.getReadBufferReadMethodCall(elementTypeReference.asSimpleTypeReference().orElseThrow(), "", arrayField)})
<#else>${elementTypeReference.asNonSimpleTypeReference().orElseThrow().name}IO.staticParse(readBuffer
<#if elementTypeReference.params.isPresent()>,
<#list elementTypeReference.params.orElseThrow() as parserArgument>
(${helper.getLanguageTypeNameForTypeReference(helper.getArgumentType(elementTypeReference, parserArgument?index), true)}) (${helper.toParseExpression(arrayField, elementTypeReference, parserArgument,parserArguments)})
<#sep>, </#sep>
</#list>
</#if>
)
</#if>
);
}
<#-- A terminated array keeps on reading data as long as the termination expression evaluates to false -->
<#elseif arrayField.isTerminatedArrayField()>
// Terminated array
${helper.getNonPrimitiveLanguageTypeNameForField(arrayField)} ${arrayField.name} = new LinkedList<>();
while(!((boolean) (${helper.toParseExpression(arrayField, helper.boolTypeReference, arrayField.loopExpression,parserArguments)}))) {
${arrayField.name}.add(<#if elementTypeReference.isSimpleTypeReference()>${helper.getReadBufferReadMethodCall(elementTypeReference.asSimpleTypeReference().orElseThrow(), "", arrayField)}<#else>${elementTypeReference.asComplexTypeReference().orElseThrow().name}IO.staticParse(readBuffer<#if arrayField.params.isPresent()>, <#list arrayField.params.orElseThrow() as parserArgument>(${helper.getLanguageTypeNameForTypeReference(helper.getArgumentType(elementTypeReference, parserArgument?index), true)}) (${helper.toParseExpression(arrayField, parserArgument,parserArguments)})<#sep>, </#sep></#list></#if>)</#if>);
<#-- After parsing, update the current position, but only if it's needed -->
<#if arrayField.loopExpression.contains("curPos")>
curPos = readBuffer.getPos() - startPos;
</#if>
}
</#if>
</#if>
<#if arrayField.name == "value">
<#assign valueDefined=true>
</#if>
<#break>
<#case "const">
<#assign constField=field.asConstField().orElseThrow()>
// Const Field (${constField.name})
${helper.getNonPrimitiveLanguageTypeNameForField(constField)} ${constField.name} = ${helper.getReadBufferReadMethodCall(constField.type.asSimpleTypeReference().orElseThrow(), "", constField)};
if(${constField.name} != ${dataIoTypeDefinition.name}.${constField.name?upper_case}) {
throw new ParseException("Expected constant value " + ${dataIoTypeDefinition.name}.${constField.name?upper_case} + " but got " + ${constField.name});
}
<#if constField.name == "value">
<#assign valueDefined=true>
</#if>
<#break>
<#case "enum">
<#assign enumField=field.asEnumField().orElseThrow()>
// Enum field (${enumField.name})
${helper.getNonPrimitiveLanguageTypeNameForField(enumField)} ${enumField.name} = ${helper.getNonPrimitiveLanguageTypeNameForField(enumField)}.enumForValue(${helper.getReadBufferReadMethodCall(helper.getEnumBaseTypeReference(enumField.type.asSimpleTypeReference().orElseThrow()), "", enumField)});
<#if enumField.name == "value">
<#assign valueDefined=true>
</#if>
<#break>
<#case "manual">
<#assign manualField=field.asManualField().orElseThrow()>
// Manual Field (${manualField.name})
${helper.getLanguageTypeNameForField(field)} ${manualField.name} = (${helper.getLanguageTypeNameForField(manualField)}) (${helper.toParseExpression(manualField, manualField.type, manualField.parseExpression,parserArguments)});
<#if manualField.name == "value">
<#assign valueDefined=true>
</#if>
<#break>
<#case "reserved">
<#assign reservedField=field.asReservedField().orElseThrow()>
// Reserved Field (Compartmentalized so the "reserved" variable can't leak)
{
${helper.getLanguageTypeNameForField(field)} reserved = ${helper.getReadBufferReadMethodCall(reservedField.type.asSimpleTypeReference().orElseThrow(), "", reservedField)};
if(reserved != ${helper.getReservedValue(reservedField)}) {
LOGGER.info("Expected constant value " + ${reservedField.referenceValue} + " but got " + reserved + " for reserved field.");
}
}
<#break>
<#case "simple">
<#assign simpleField=field.asSimpleField().orElseThrow()>
<#if helper.isEnumField(simpleField)>
// Enum field (${simpleField.name})
${helper.getNonPrimitiveLanguageTypeNameForField(simpleField)} ${simpleField.name} = ${helper.getNonPrimitiveLanguageTypeNameForField(simpleField)}.enumForValue(${helper.getReadBufferReadMethodCall(helper.getEnumBaseTypeReference(simpleField.type), "", simpleField)});
<#else>
// Simple Field (${simpleField.name})
${helper.getNonPrimitiveLanguageTypeNameForField(simpleField)} ${simpleField.name} = <#if simpleField.type.isSimpleTypeReference()>${helper.getReadBufferReadMethodCall(simpleField.type.asSimpleTypeReference().orElseThrow(), "", simpleField)}<#else>${simpleField.type.asComplexTypeReference().orElseThrow().name}IO.staticParse(readBuffer<#if simpleField.params.isPresent()>, <#list field.params.orElseThrow() as parserArgument>(${helper.getLanguageTypeNameForTypeReference(helper.getArgumentType(simpleField.type, parserArgument?index), true)}) (${helper.toParseExpression(simpleField, simpleField.type, parserArgument,parserArguments)})<#sep>, </#sep></#list></#if>)</#if>;
</#if>
<#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"))) ||
((case.name == "DATE_AND_TIME") && (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">
<#assign valueDefined=true>
</#if>
<#break>
</#switch>
</#list>
<#if case.name == "Struct">
<#-- In this case we need to wrap each field in a PlcValue that matches it's natural type -->
Map<String, PlcValue> _map = new HashMap<>();
<#list case.fields as field>
<#if field.isArrayField()>
<#assign field=field.asArrayField().orElseThrow()>
_map.put("${field.name}", new PlcList(${field.name}));
<#elseif field.isPropertyField()>
<#assign field=field.asPropertyField().orElseThrow()>
<#switch helper.getLanguageTypeNameForTypeReference(field.type)>
<#case "Boolean">
_map.put("${field.name}", new PlcBOOL(${field.name}));
<#break>
<#case "Byte">
_map.put("${field.name}", new PlcSINT(${field.name}));
<#break>
<#case "Short">
_map.put("${field.name}", new PlcINT(${field.name}));
<#break>
<#case "Integer">
_map.put("${field.name}", new PlcDINT(${field.name}));
<#break>
<#case "Long">
_map.put("${field.name}", new PlcLINT(${field.name}));
<#break>
<#case "BigInteger">
_map.put("${field.name}", new PlcBigInteger(${field.name}));
<#break>
<#case "Float">
_map.put("${field.name}", new PlcREAL(${field.name}));
<#break>
<#case "Double">
_map.put("${field.name}", new PlcLREAL(${field.name}));
<#break>
<#case "BigDecimal">
_map.put("${field.name}", new PlcBigDecimal(${field.name}));
<#break>
<#case "String">
_map.put("${field.name}", new PlcSTRING(${field.name}));
<#break>
<#case "LocalTime">
_map.put("${field.name}", new PlcTIME_OF_DAY(${field.name}));
<#break>
<#case "LocalDate">
_map.put("${field.name}", new PlcDATE(${field.name}));
<#break>
<#case "LocalDateTime">
_map.put("${field.name}", new PlcDATE_AND_TIME(${field.name}));
<#break>
</#switch>
</#if>
</#list>
<#assign valueDefined=true>
</#if>
<#switch case.name>
<#case "TIME">
<#if helper.hasFieldsWithNames(case.fields, "milliseconds")>
return PlcTIME.ofMilliseconds(milliseconds);
</#if>
<#break>
<#case "LTIME">
<#if helper.hasFieldsWithNames(case.fields, "nanoseconds")>
return PlcLTIME.ofNanoseconds(nanoseconds);
</#if>
<#break>
<#case "DATE">
<#if helper.hasFieldsWithNames(case.fields, "secondsSinceEpoch")>
return PlcDATE.ofSecondsSinceEpoch(secondsSinceEpoch);
<#elseif helper.hasFieldsWithNames(case.fields, "daysSinceEpoch")>
return PlcDATE.ofDaysSinceEpoch(daysSinceEpoch);
<#elseif helper.hasFieldsWithNames(case.fields, "daysSinceSiemensEpoch")>
return PlcDATE.ofDaysSinceSiemensEpoch(daysSinceSiemensEpoch);
</#if>
<#break>
<#case "LDATE">
<#if helper.hasFieldsWithNames(case.fields, "nanosecondsSinceEpoch")>
return PlcLDATE.ofNanosecondsSinceEpoch(nanosecondsSinceEpoch);
</#if>
<#break>
<#case "TIME_OF_DAY">
<#if helper.hasFieldsWithNames(case.fields, "millisecondsSinceMidnight")>
return PlcTIME_OF_DAY.ofMillisecondsSinceMidnight(millisecondsSinceMidnight);
</#if>
<#break>
<#case "LTIME_OF_DAY">
<#if helper.hasFieldsWithNames(case.fields, "nanosecondsSinceMidnight")>
return PlcLTIME_OF_DAY.ofNanosecondsSinceMidnight(nanosecondsSinceMidnight);
</#if>
<#break>
<#case "DATE_AND_TIME">
<#if helper.hasFieldsWithNames(case.fields, "year", "month", "day", "hour", "minutes", "seconds", "nanoseconds")>
return PlcDATE_AND_TIME.ofSegments(year.intValue(), (month == 0) ? 1 : month.intValue(), (day == 0) ? 1 : day.intValue(), hour.intValue(), minutes.intValue(), seconds.intValue(), nanoseconds.intValue());
<#elseif helper.hasFieldsWithNames(case.fields, "secondsSinceEpoch")>
return PlcDATE_AND_TIME.ofSecondsSinceEpoch(secondsSinceEpoch);
</#if>
<#break>
<#case "LDATE_AND_TIME">
<#if helper.hasFieldsWithNames(case.fields, "nanosecondsSinceEpoch")>
return PlcLDATE_AND_TIME.ofNanosecondsSinceEpoch(nanosecondsSinceEpoch);
</#if>
<#break>
<#case "Struct">
return new PlcStruct(_map);
<#break>
<#case "List">
return new PlcList(value);
<#break>
<#default>
return new Plc${case.name}(value);
</#switch>
}<#sep> else </#sep></#list>
<#if !defaultCaseOutput>
return null;
</#if>
}
<#if outputFlavor != "passive">
public static void staticSerialize(WriteBuffer writeBuffer, PlcValue _value<#if type.parserArguments.isPresent()>, <#list type.parserArguments.orElseThrow() as parserArgument>${helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)} ${parserArgument.name}<#sep>, </#sep></#list></#if>) throws SerializationException {
staticSerialize(writeBuffer, _value<#if type.parserArguments.isPresent()>, <#list type.parserArguments.orElseThrow() as parserArgument>${parserArgument.name}<#sep>, </#sep></#list></#if>, ByteOrder.BIG_ENDIAN);
}
public static void staticSerialize(WriteBuffer writeBuffer, PlcValue _value<#if type.parserArguments.isPresent()>, <#list type.parserArguments.orElseThrow() as parserArgument>${helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)} ${parserArgument.name}<#sep>, </#sep></#list></#if>, ByteOrder byteOrder) throws SerializationException {
<#assign defaultCaseOutput=false>
<#assign dataIoTypeDefinition=type.asDataIoTypeDefinition().orElseThrow()>
<#list dataIoTypeDefinition.switchField.orElseThrow().cases as case>
<@compress single_line=true>
<#if case.discriminatorValueTerms?has_content>
if(
<#list case.discriminatorValueTerms as discriminatorValueTerm>
<#assign discriminatorExpression=dataIoTypeDefinition.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index].asLiteral().orElseThrow().asVariableLiteral().orElseThrow()>
<#assign discriminatorType=helper.getDiscriminatorTypes()[discriminatorExpression.name]>
EvaluationHelper.equals(
${helper.toParseExpression(dataIoTypeDefinition.switchField.orElseThrow(), discriminatorType, discriminatorExpression, parserArguments)},
<#if discriminatorType.isEnumTypeReference()>
${helper.getLanguageTypeNameForTypeReference(discriminatorType)}.${helper.toParseExpression(dataIoTypeDefinition.switchField.orElseThrow(), discriminatorType, discriminatorValueTerm, parserArguments)}
<#else>
${helper.toParseExpression(dataIoTypeDefinition.switchField.orElseThrow(), discriminatorType, discriminatorValueTerm, parserArguments)}
</#if>
)
<#sep> && </#sep>
</#list>
)
<#else>
<#assign defaultCaseOutput=true>
</#if>
</@compress> { // ${case.name}
<#list case.fields as field>
<#switch field.typeName>
<#case "array">
<#assign arrayField=field.asArrayField().orElseThrow()>
<#assign elementTypeReference=arrayField.type.elementTypeReference>
PlcList values = (PlcList) _value;
<#if case.name == "Struct">
for (PlcValue val : ((List<PlcValue>) values.getStruct().get("${arrayField.name}").getList())) {
<#if elementTypeReference.isByteBased()>
${helper.getLanguageTypeNameForField(arrayField)} value = (${helper.getLanguageTypeNameForField(arrayField)}) val.getRaw();
writeBuffer.writeByteArray("", value);
<#else>
${helper.getLanguageTypeNameForField(arrayField)} value = (${helper.getLanguageTypeNameForField(arrayField)}) val.get${helper.getLanguageTypeNameForField(arrayField)?cap_first}();
${helper.getWriteBufferWriteMethodCall(elementTypeReference.asSimpleTypeReference().orElseThrow(), "value", arrayField)};
</#if>
}
<#else>
for (PlcValue val : ((List<PlcValue>) values.getList())) {
<#if elementTypeReference.isByteBased()>
byte[] value = (byte[]) val.getRaw();
writeBuffer.writeByteArray("", value);
<#else>
${helper.getLanguageTypeNameForTypeReference(elementTypeReference)} value = (${helper.getLanguageTypeNameForTypeReference(elementTypeReference)}) val.get${helper.getLanguageTypeNameForTypeReference(elementTypeReference)?cap_first}();
${helper.getWriteBufferWriteMethodCall(elementTypeReference.asSimpleTypeReference().orElseThrow(), "(" + arrayField.name + ")", arrayField)};
</#if>
}
</#if>
<#if case.name == "BOOL">
while (writeBuffer.getPos() < writeBuffer.getData().length) {
writeBuffer.writeBit(false);
}
</#if>
<#break>
<#case "const">
<#assign constField=field.asConstField().orElseThrow()>
// Const Field (${constField.name})
${helper.getWriteBufferWriteMethodCall(constField.type.asSimpleTypeReference().orElseThrow(), constField.referenceValue, constField)};
<#break>
<#case "enum">
<#assign enumField=field.asEnumField().orElseThrow()>
// Enum field (${enumField.name})
${helper.getLanguageTypeNameForField(field)} ${enumField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${enumField.name?cap_first}();
${helper.getWriteBufferWriteMethodCall(helper.getEnumBaseTypeReference(field.asTypedField().orElseThrow().type), "(" + enumField.name + ".getValue())", enumField)};
<#break>
<#case "manual">
<#assign manualField=field.asManualField().orElseThrow()>
// Manual Field (${manualField.name})
${helper.toSerializationExpression(manualField, manualField.type, manualField.serializeExpression, type.parserArguments.orElse(null))};
<#break>
<#case "reserved">
<#assign reservedField=field.asReservedField().orElseThrow()>
// Reserved Field
${helper.getWriteBufferWriteMethodCall(reservedField.type.asSimpleTypeReference().orElseThrow(), helper.getReservedValue(reservedField), reservedField)};
<#break>
<#case "simple">
<#assign simpleField=field.asSimpleField().orElseThrow()>
// Simple Field (${simpleField.name})
<#if case.name == "Struct">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.getStruct().get("${simpleField.name}").get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "value">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "secondsSinceEpoch">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "daysSinceEpoch">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "daysSinceSiemensEpoch">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "year">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "month">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "day">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "dayOfWeek">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "hour">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "minutes">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "seconds">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "milliseconds">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "millisecondsSinceMidnight">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "nanoseconds">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "nanosecondsSinceMidnight">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
<#elseif simpleField.name == "nanosecondsSinceEpoch">
${helper.getLanguageTypeNameForField(simpleField)} ${simpleField.name} = (${helper.getLanguageTypeNameForField(field)}) _value.get${helper.getLanguageTypeNameForField(simpleField)?cap_first}();
</#if>
<#if simpleField.type.isSimpleTypeReference()>
${helper.getWriteBufferWriteMethodCall(simpleField.type.asSimpleTypeReference().orElseThrow(), "(" + simpleField.name + ")", simpleField)};
<#else>
${simpleField.type.asComplexTypeReference().orElseThrow().name}IO.staticSerialize(writeBuffer, ${simpleField.name});
</#if>
<#break>
</#switch>
</#list>
}<#sep> else </#sep></#list>
}
</#if>
public static int getLengthInBytes(PlcValue _value<#if type.parserArguments.isPresent()>, <#list type.parserArguments.orElseThrow() as parserArgument>${helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)} ${parserArgument.name}<#sep>, </#sep></#list></#if>) {
return (int) Math.ceil((float) getLengthInBits(_value<#if type.parserArguments.isPresent()>, <#list type.parserArguments.orElseThrow() as parserArgument>${parserArgument.name}<#sep>, </#sep></#list></#if>) / 8.0);
}
public static int getLengthInBits(PlcValue _value<#if type.parserArguments.isPresent()>, <#list type.parserArguments.orElseThrow() as parserArgument>${helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)} ${parserArgument.name}<#sep>, </#sep></#list></#if>) {
int sizeInBits = 0;
<#assign defaultCaseOutput=false>
<#assign dataIoTypeDefinition=type.asDataIoTypeDefinition().orElseThrow()>
<#list dataIoTypeDefinition.switchField.orElseThrow().cases as case>
<@compress single_line=true>
<#if case.discriminatorValueTerms?has_content>
if(
<#list case.discriminatorValueTerms as discriminatorValueTerm>
<#assign discriminatorExpression=dataIoTypeDefinition.switchField.orElseThrow().discriminatorExpressions[discriminatorValueTerm?index].asLiteral().orElseThrow().asVariableLiteral().orElseThrow()>
<#assign discriminatorType=helper.getDiscriminatorTypes()[discriminatorExpression.name]>
EvaluationHelper.equals(
${helper.toParseExpression(dataIoTypeDefinition.switchField.orElseThrow(), discriminatorType, discriminatorExpression, parserArguments)},
<#if discriminatorType.isEnumTypeReference()>
${helper.getLanguageTypeNameForTypeReference(discriminatorType)}.${helper.toParseExpression(dataIoTypeDefinition.switchField.orElseThrow(), discriminatorType, discriminatorValueTerm, parserArguments)}
<#else>
${helper.toParseExpression(dataIoTypeDefinition.switchField.orElseThrow(), discriminatorType, discriminatorValueTerm, parserArguments)}
</#if>
)
<#sep> && </#sep>
</#list>
)
<#else>
<#assign defaultCaseOutput=true>
</#if>
</@compress> { // ${case.name}
<#list case.fields as field>
<#switch field.typeName>
<#case "array">
<#assign arrayField=field.asArrayField().orElseThrow()>
<#assign elementTypeReference=arrayField.type.elementTypeReference>
PlcList values = (PlcList) _value;
<#if case.name == "Struct">
// TODO: Finish this!
<#elseif elementTypeReference.isComplexTypeReference()>
// TODO: Finish this!
<#else>
sizeInBits += values.getList().size() * ${elementTypeReference.asSimpleTypeReference().orElseThrow().sizeInBits};
</#if>
<#break>
<#case "const">
<#assign constField=field.asConstField().orElseThrow()>
// Const Field (${constField.name})
<#-- const fields are only simple type -->
sizeInBits += ${constField.type.asSimpleTypeReference().orElseThrow().sizeInBits};
<#break>
<#case "enum">
<#assign enumField=field.asEnumField().orElseThrow()>
// Enum field (${enumField.name})
sizeInBits += ${helper.getEnumFieldSimpleTypeReference(enumField.type, enumField.fieldName).sizeInBits};
<#break>
<#case "manual">
<#assign manualField=field.asManualField().orElseThrow()>
// Manual Field (${manualField.name})
sizeInBits += ${helper.toSerializationExpression(manualField, helper.intTypeReference, manualField.lengthExpression, type.parserArguments.orElse(null))};
<#break>
<#case "reserved">
<#assign reservedField=field.asReservedField().orElseThrow()>
// Reserved Field
sizeInBits += ${reservedField.type.asSimpleTypeReference().orElseThrow().sizeInBits};
<#break>
<#case "simple">
<#assign simpleField=field.asSimpleField().orElseThrow()>
// Simple Field (${simpleField.name})
sizeInBits += ${simpleField.type.asSimpleTypeReference().orElseThrow().sizeInBits};
<#break>
</#switch>
</#list>
}<#sep> else </#sep></#list>
return sizeInBits;
}
}
</#outputformat>