22
22
import uk .co .real_logic .sbe .ir .Token ;
23
23
24
24
import java .io .UnsupportedEncodingException ;
25
- import java .nio .ByteOrder ;
26
25
import java .util .ArrayList ;
27
26
import java .util .List ;
28
27
32
31
public class IrGenerator
33
32
{
34
33
private final List <Token > tokenList = new ArrayList <>();
35
- private ByteOrder byteOrder = ByteOrder .LITTLE_ENDIAN ;
36
-
34
+ private MessageSchema schema ;
37
35
/**
38
36
* Generate a complete {@link uk.co.real_logic.sbe.ir.Ir} for a given schema.
39
37
*
@@ -43,6 +41,8 @@ public class IrGenerator
43
41
*/
44
42
public Ir generate (final MessageSchema schema , final String namespace )
45
43
{
44
+ this .schema = schema ;
45
+
46
46
final List <Token > headerTokens = generateForHeader (schema );
47
47
final Ir ir = new Ir (
48
48
schema .packageName (),
@@ -76,7 +76,6 @@ public Ir generate(final MessageSchema schema)
76
76
private List <Token > generateForMessage (final MessageSchema schema , final long messageId )
77
77
{
78
78
tokenList .clear ();
79
- byteOrder = schema .byteOrder ();
80
79
81
80
final Message msg = schema .getMessage (messageId );
82
81
@@ -91,7 +90,6 @@ private List<Token> generateForHeader(final MessageSchema schema)
91
90
{
92
91
tokenList .clear ();
93
92
94
- byteOrder = schema .byteOrder ();
95
93
add (schema .messageHeader (), 0 , null );
96
94
97
95
return tokenList ;
@@ -236,7 +234,7 @@ private void add(final CompositeType type, final int currOffset, final Field fie
236
234
237
235
if (elementType instanceof EncodedDataType )
238
236
{
239
- add ((EncodedDataType )elementType , offset , null );
237
+ add ((EncodedDataType )elementType , offset );
240
238
}
241
239
else if (elementType instanceof EnumType )
242
240
{
@@ -263,7 +261,7 @@ private void add(final EnumType type, final int offset, final Field field)
263
261
final Encoding .Builder encodingBuilder = new Encoding .Builder ()
264
262
.primitiveType (encodingType )
265
263
.semanticType (semanticTypeOf (type , field ))
266
- .byteOrder (byteOrder );
264
+ .byteOrder (schema . byteOrder () );
267
265
268
266
if (type .presence () == Presence .OPTIONAL )
269
267
{
@@ -307,7 +305,7 @@ private void add(final EnumType.ValidValue value, final PrimitiveType encodingTy
307
305
.deprecated (value .deprecated ())
308
306
.description (value .description ())
309
307
.encoding (new Encoding .Builder ()
310
- .byteOrder (byteOrder )
308
+ .byteOrder (schema . byteOrder () )
311
309
.primitiveType (encodingType )
312
310
.constValue (value .primitiveValue ())
313
311
.build ());
@@ -360,27 +358,69 @@ private void add(final SetType.Choice value, final PrimitiveType encodingType)
360
358
.deprecated (value .deprecated ())
361
359
.encoding (new Encoding .Builder ()
362
360
.constValue (value .primitiveValue ())
363
- .byteOrder (byteOrder )
361
+ .byteOrder (schema . byteOrder () )
364
362
.primitiveType (encodingType )
365
363
.build ());
366
364
367
365
tokenList .add (builder .build ());
368
366
}
369
367
370
- private void add (final EncodedDataType type , final int offset , final Field field )
368
+ private void add (final EncodedDataType type , final int offset )
371
369
{
372
370
final Encoding .Builder encodingBuilder = new Encoding .Builder ()
373
371
.primitiveType (type .primitiveType ())
374
- .byteOrder (byteOrder )
375
- .semanticType (semanticTypeOf (type , field ))
372
+ .byteOrder (schema .byteOrder ())
376
373
.characterEncoding (type .characterEncoding ());
377
374
378
- if (null != field )
375
+ final Token .Builder tokenBuilder = new Token .Builder ()
376
+ .signal (Signal .ENCODING )
377
+ .name (type .name ())
378
+ .referencedName (type .referencedName ())
379
+ .size (type .encodedLength ())
380
+ .description (type .description ())
381
+ .version (type .sinceVersion ())
382
+ .deprecated (type .deprecated ())
383
+ .offset (offset );
384
+
385
+ switch (type .presence ())
379
386
{
380
- encodingBuilder .epoch (field .epoch ());
381
- encodingBuilder .timeUnit (field .timeUnit ());
387
+ case REQUIRED :
388
+ encodingBuilder
389
+ .presence (Encoding .Presence .REQUIRED )
390
+ .minValue (type .minValue ())
391
+ .maxValue (type .maxValue ());
392
+ break ;
393
+
394
+ case OPTIONAL :
395
+ encodingBuilder
396
+ .presence (Encoding .Presence .OPTIONAL )
397
+ .minValue (type .minValue ())
398
+ .maxValue (type .maxValue ())
399
+ .nullValue (type .nullValue ());
400
+ break ;
401
+
402
+ case CONSTANT :
403
+ encodingBuilder
404
+ .presence (Encoding .Presence .CONSTANT )
405
+ .constValue (type .constVal ());
406
+ break ;
382
407
}
383
408
409
+ final Token token = tokenBuilder .encoding (encodingBuilder .build ()).build ();
410
+
411
+ tokenList .add (token );
412
+ }
413
+
414
+ private void add (final EncodedDataType type , final int offset , final Field field )
415
+ {
416
+ final Encoding .Builder encodingBuilder = new Encoding .Builder ()
417
+ .primitiveType (type .primitiveType ())
418
+ .byteOrder (schema .byteOrder ())
419
+ .semanticType (semanticTypeOf (type , field ))
420
+ .characterEncoding (type .characterEncoding ())
421
+ .timeUnit (field .timeUnit ())
422
+ .epoch (field .epoch ());
423
+
384
424
final Token .Builder tokenBuilder = new Token .Builder ()
385
425
.signal (Signal .ENCODING )
386
426
.name (type .name ())
@@ -391,12 +431,12 @@ private void add(final EncodedDataType type, final int offset, final Field field
391
431
.deprecated (type .deprecated ())
392
432
.offset (offset );
393
433
394
- if (null != field && !( field .type () instanceof CompositeType ) )
434
+ if (field .type () instanceof CompositeType )
395
435
{
396
436
tokenBuilder .version (Math .max (field .sinceVersion (), type .sinceVersion ()));
397
437
}
398
438
399
- switch (type .presence ())
439
+ switch (field .presence ())
400
440
{
401
441
case REQUIRED :
402
442
encodingBuilder
@@ -414,9 +454,11 @@ private void add(final EncodedDataType type, final int offset, final Field field
414
454
break ;
415
455
416
456
case CONSTANT :
457
+ final String valueRef = field .valueRef ();
458
+ tokenBuilder .size (0 );
417
459
encodingBuilder
418
460
.presence (Encoding .Presence .CONSTANT )
419
- .constValue (type .constVal ());
461
+ .constValue (valueRef != null ? lookupValueRef ( valueRef ) : type .constVal ());
420
462
break ;
421
463
}
422
464
@@ -425,6 +467,18 @@ private void add(final EncodedDataType type, final int offset, final Field field
425
467
tokenList .add (token );
426
468
}
427
469
470
+ private PrimitiveValue lookupValueRef (final String valueRef )
471
+ {
472
+ final int periodIndex = valueRef .indexOf ('.' );
473
+ final String valueRefType = valueRef .substring (0 , periodIndex );
474
+ final String validValueName = valueRef .substring (periodIndex + 1 );
475
+
476
+ final EnumType enumType = (EnumType )schema .getType (valueRefType );
477
+ final EnumType .ValidValue validValue = enumType .getValidValue (validValueName );
478
+
479
+ return validValue .primitiveValue ();
480
+ }
481
+
428
482
private static String semanticTypeOf (final Type type , final Field field )
429
483
{
430
484
final String typeSemanticType = null != type ? type .semanticType () : null ;
0 commit comments