private int generateFieldEncodeDecode ( final List < Token > tokens , final char varName , final int currentOffset , final StringBuilder encode , final StringBuilder decode , final StringBuilder rc , final StringBuilder init ) { final Token signalToken = tokens . get ( 0 ) ; final Token encodingToken = tokens . get ( 1 ) ; final String propertyName = formatPropertyName ( signalToken . name ( ) ) ; int gap = 0 ; // for offset calculations switch ( encodingToken . signal ( ) ) { case BEGIN_COMPOSITE : case BEGIN_ENUM : case BEGIN_SET : gap = signalToken . offset ( ) - currentOffset ; encode . append ( generateEncodeOffset ( gap , "" ) ) ; decode . append ( generateDecodeOffset ( gap , "" ) ) ; // Encode of a constant is a nullop, decode is assignment if ( signalToken . isConstantEncoding ( ) ) { decode . append ( String . format ( "\t%1$s.%2$s = %3$s\n" , varName , propertyName , signalToken . encoding ( ) . constValue ( ) ) ) ; init . append ( String . format ( "\t%1$s.%2$s = %3$s\n" , varName , propertyName , signalToken . encoding ( ) . constValue ( ) ) ) ; } else { encode . append ( String . format ( "\tif err := %1$s.%2$s.Encode(_m, _w); err != nil {\n" + "\t\treturn err\n" + "\t}\n" , varName , propertyName ) ) ; decode . append ( String . format ( "\tif %1$s.%2$sInActingVersion(actingVersion) {\n" + "\t\tif err := %1$s.%2$s.Decode(_m, _r, actingVersion); err != nil {\n" + "\t\t\treturn err\n" + "\t\t}\n" + "\t}\n" , varName , propertyName ) ) ; } if ( encodingToken . signal ( ) == Signal . BEGIN_ENUM ) { rc . append ( String . format ( "\tif err := %1$s.%2$s.RangeCheck(actingVersion, schemaVersion); err != nil {\n" + "\t\treturn err\n" + "\t}\n" , varName , propertyName ) ) ; } break ; case ENCODING : gap = encodingToken . offset ( ) - currentOffset ; encode . append ( generateEncodeOffset ( gap , "" ) ) ; decode . append ( generateDecodeOffset ( gap , "" ) ) ; final String primitive = varName + "." + propertyName ; // Encode of a constant is a nullop and we want to // initialize constant values. // (note: constancy is determined by the type's token) if ( encodingToken . isConstantEncoding ( ) ) { generateConstantInitPrimitive ( init , primitive , encodingToken ) ; } else { generateEncodePrimitive ( encode , varName , formatPropertyName ( signalToken . name ( ) ) , encodingToken ) ; } // Optional tokens get initialized to NullValue // (note: optionality is determined by the field's token) if ( signalToken . isOptionalEncoding ( ) ) { generateOptionalInitPrimitive ( init , primitive , encodingToken ) ; } generateDecodePrimitive ( decode , primitive , encodingToken ) ; generateRangeCheckPrimitive ( rc , primitive , encodingToken , signalToken . isOptionalEncoding ( ) ) ; break ; } return encodingToken . encodedLength ( ) + gap ; }
Returns how many extra tokens to skip over