2222import uk .co .real_logic .sbe .ir .Token ;
2323
2424import java .io .UnsupportedEncodingException ;
25- import java .nio .ByteOrder ;
2625import java .util .ArrayList ;
2726import java .util .List ;
2827
3231public class IrGenerator
3332{
3433 private final List <Token > tokenList = new ArrayList <>();
35- private ByteOrder byteOrder = ByteOrder .LITTLE_ENDIAN ;
36-
34+ private MessageSchema schema ;
3735 /**
3836 * Generate a complete {@link uk.co.real_logic.sbe.ir.Ir} for a given schema.
3937 *
@@ -43,6 +41,8 @@ public class IrGenerator
4341 */
4442 public Ir generate (final MessageSchema schema , final String namespace )
4543 {
44+ this .schema = schema ;
45+
4646 final List <Token > headerTokens = generateForHeader (schema );
4747 final Ir ir = new Ir (
4848 schema .packageName (),
@@ -76,7 +76,6 @@ public Ir generate(final MessageSchema schema)
7676 private List <Token > generateForMessage (final MessageSchema schema , final long messageId )
7777 {
7878 tokenList .clear ();
79- byteOrder = schema .byteOrder ();
8079
8180 final Message msg = schema .getMessage (messageId );
8281
@@ -91,7 +90,6 @@ private List<Token> generateForHeader(final MessageSchema schema)
9190 {
9291 tokenList .clear ();
9392
94- byteOrder = schema .byteOrder ();
9593 add (schema .messageHeader (), 0 , null );
9694
9795 return tokenList ;
@@ -236,7 +234,7 @@ private void add(final CompositeType type, final int currOffset, final Field fie
236234
237235 if (elementType instanceof EncodedDataType )
238236 {
239- add ((EncodedDataType )elementType , offset , null );
237+ add ((EncodedDataType )elementType , offset );
240238 }
241239 else if (elementType instanceof EnumType )
242240 {
@@ -263,7 +261,7 @@ private void add(final EnumType type, final int offset, final Field field)
263261 final Encoding .Builder encodingBuilder = new Encoding .Builder ()
264262 .primitiveType (encodingType )
265263 .semanticType (semanticTypeOf (type , field ))
266- .byteOrder (byteOrder );
264+ .byteOrder (schema . byteOrder () );
267265
268266 if (type .presence () == Presence .OPTIONAL )
269267 {
@@ -307,7 +305,7 @@ private void add(final EnumType.ValidValue value, final PrimitiveType encodingTy
307305 .deprecated (value .deprecated ())
308306 .description (value .description ())
309307 .encoding (new Encoding .Builder ()
310- .byteOrder (byteOrder )
308+ .byteOrder (schema . byteOrder () )
311309 .primitiveType (encodingType )
312310 .constValue (value .primitiveValue ())
313311 .build ());
@@ -360,27 +358,69 @@ private void add(final SetType.Choice value, final PrimitiveType encodingType)
360358 .deprecated (value .deprecated ())
361359 .encoding (new Encoding .Builder ()
362360 .constValue (value .primitiveValue ())
363- .byteOrder (byteOrder )
361+ .byteOrder (schema . byteOrder () )
364362 .primitiveType (encodingType )
365363 .build ());
366364
367365 tokenList .add (builder .build ());
368366 }
369367
370- private void add (final EncodedDataType type , final int offset , final Field field )
368+ private void add (final EncodedDataType type , final int offset )
371369 {
372370 final Encoding .Builder encodingBuilder = new Encoding .Builder ()
373371 .primitiveType (type .primitiveType ())
374- .byteOrder (byteOrder )
375- .semanticType (semanticTypeOf (type , field ))
372+ .byteOrder (schema .byteOrder ())
376373 .characterEncoding (type .characterEncoding ());
377374
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 ())
379386 {
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 ;
382407 }
383408
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+
384424 final Token .Builder tokenBuilder = new Token .Builder ()
385425 .signal (Signal .ENCODING )
386426 .name (type .name ())
@@ -391,12 +431,12 @@ private void add(final EncodedDataType type, final int offset, final Field field
391431 .deprecated (type .deprecated ())
392432 .offset (offset );
393433
394- if (null != field && !( field .type () instanceof CompositeType ) )
434+ if (field .type () instanceof CompositeType )
395435 {
396436 tokenBuilder .version (Math .max (field .sinceVersion (), type .sinceVersion ()));
397437 }
398438
399- switch (type .presence ())
439+ switch (field .presence ())
400440 {
401441 case REQUIRED :
402442 encodingBuilder
@@ -414,9 +454,11 @@ private void add(final EncodedDataType type, final int offset, final Field field
414454 break ;
415455
416456 case CONSTANT :
457+ final String valueRef = field .valueRef ();
458+ tokenBuilder .size (0 );
417459 encodingBuilder
418460 .presence (Encoding .Presence .CONSTANT )
419- .constValue (type .constVal ());
461+ .constValue (valueRef != null ? lookupValueRef ( valueRef ) : type .constVal ());
420462 break ;
421463 }
422464
@@ -425,6 +467,18 @@ private void add(final EncodedDataType type, final int offset, final Field field
425467 tokenList .add (token );
426468 }
427469
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+
428482 private static String semanticTypeOf (final Type type , final Field field )
429483 {
430484 final String typeSemanticType = null != type ? type .semanticType () : null ;
0 commit comments