@@ -1179,8 +1179,10 @@ private int generateGroupEncodeDecode(
11791179 final  String  propertyName  = formatPropertyName (signalToken .name ());
11801180 final  Token  blockLengthToken  = Generators .findFirst ("blockLength" , tokens , 0 );
11811181 final  Token  numInGroupToken  = Generators .findFirst ("numInGroup" , tokens , 0 );
1182+  final  int  blockLengthOffset  = blockLengthToken .offset ();
11821183 final  String  blockLengthType  = golangTypeName (blockLengthToken .encoding ().primitiveType ());
11831184 final  String  blockLengthMarshalType  = golangMarshalType (blockLengthToken .encoding ().primitiveType ());
1185+  final  int  numInGroupOffset  = numInGroupToken .offset ();
11841186 final  String  numInGroupType  = golangTypeName (numInGroupToken .encoding ().primitiveType ());
11851187 final  String  numInGroupMarshalType  = golangMarshalType (numInGroupToken .encoding ().primitiveType ());
11861188
@@ -1189,16 +1191,32 @@ private int generateGroupEncodeDecode(
11891191 encode .append (generateEncodeOffset (gap , "" ));
11901192 decode .append (generateDecodeOffset (gap , "" ));
11911193
1192-  // Write/Read the group header 
1193-  encode .append (String .format (
1194-  "\n \t var %7$sBlockLength %1$s = %2$d\n "  +
1195-  "\t var %7$sNumInGroup %3$s = %3$s(len(%4$s.%5$s))\n "  +
1194+  // Write block length 
1195+  final  String  encBlockLengthTmpl  =
1196+  "\t var %7$sBlockLength %1$s = %2$d\n "  +
11961197 "\t if err := _m.Write%6$s(_w, %7$sBlockLength); err != nil {\n "  +
11971198 "\t \t return err\n "  +
1198-  "\t }\n "  +
1199+  "\t }\n " ;
1200+ 
1201+  // Write number of elements in group 
1202+  final  String  encNumInGroupTmpl  =
1203+  "\t var %7$sNumInGroup %3$s = %3$s(len(%4$s.%5$s))\n "  +
11991204 "\t if err := _m.Write%8$s(_w, %7$sNumInGroup); err != nil {\n "  +
12001205 "\t \t return err\n "  +
1201-  "\t }\n " ,
1206+  "\t }\n " ;
1207+ 
1208+  // Order write based on offset 
1209+  String  encGrpMetaTmpl  = "\n " ;
1210+  if  (blockLengthOffset  < numInGroupOffset )
1211+  {
1212+  encGrpMetaTmpl  = encBlockLengthTmpl  + encNumInGroupTmpl ;
1213+  }
1214+  else 
1215+  {
1216+  encGrpMetaTmpl  = encNumInGroupTmpl  + encBlockLengthTmpl ;
1217+  }
1218+ 
1219+  encode .append (String .format (encGrpMetaTmpl ,
12021220 blockLengthType ,
12031221 signalToken .encodedLength (),
12041222 numInGroupType ,
@@ -1208,7 +1226,7 @@ private int generateGroupEncodeDecode(
12081226 propertyName ,
12091227 numInGroupMarshalType ));
12101228
1211-  // Write/Read  the group itself 
1229+  // Write the group itself 
12121230 encode .append (String .format (
12131231 "\t for _, prop := range %1$s.%2$s {\n "  +
12141232 "\t \t if err := prop.Encode(_m, _w); err != nil {\n "  +
@@ -1217,26 +1235,50 @@ private int generateGroupEncodeDecode(
12171235 varName ,
12181236 toUpperFirstChar (signalToken .name ())));
12191237
1220-  // Read length/num  
1238+  // Check version  
12211239 decode .append (String .format (
12221240 "\n "  +
1223-  "\t if %1$s.%2$sInActingVersion(actingVersion) {\n "  +
1241+  "\t if %1$s.%2$sInActingVersion(actingVersion) {\n " ,
1242+  varName ,
1243+  propertyName ,
1244+  blockLengthType ,
1245+  numInGroupType ,
1246+  blockLengthMarshalType ,
1247+  numInGroupMarshalType ));
1248+ 
1249+  // Read block length 
1250+  final  String  decBlockLengthTmpl  =
12241251 "\t \t var %2$sBlockLength %3$s\n "  +
1225-  "\t \t var %2$sNumInGroup %4$s\n "  +
12261252 "\t \t if err := _m.Read%5$s(_r, &%2$sBlockLength); err != nil {\n "  +
12271253 "\t \t \t return err\n "  +
1228-  "\t \t }\n "  +
1254+  "\t \t }\n " ;
1255+ 
1256+  // Read number of elements in group 
1257+  final  String  decNumInGroupTmpl  =
1258+  "\t \t var %2$sNumInGroup %4$s\n "  +
12291259 "\t \t if err := _m.Read%6$s(_r, &%2$sNumInGroup); err != nil {\n "  +
12301260 "\t \t \t return err\n "  +
1231-  "\t \t }\n " ,
1261+  "\t \t }\n " ;
1262+ 
1263+  // Order read based on offset 
1264+  String  decGrpMetaTmpl  = "\n " ;
1265+  if  (blockLengthOffset  < numInGroupOffset )
1266+  {
1267+  decGrpMetaTmpl  = decBlockLengthTmpl  + decNumInGroupTmpl ;
1268+  }
1269+  else 
1270+  {
1271+  decGrpMetaTmpl  = decNumInGroupTmpl  + decBlockLengthTmpl ;
1272+  }
1273+  decode .append (String .format (decGrpMetaTmpl ,
12321274 varName ,
12331275 propertyName ,
12341276 blockLengthType ,
12351277 numInGroupType ,
12361278 blockLengthMarshalType ,
12371279 numInGroupMarshalType ));
12381280
1239-  // Read num elements  
1281+  // Read the group itself  
12401282 decode .append (String .format (
12411283 "\t \t if cap(%1$c.%2$s) < int(%2$sNumInGroup) {\n "  +
12421284 "\t \t \t %1$s.%2$s = make([]%3$s%2$s, %2$sNumInGroup)\n "  +
0 commit comments