@@ -851,7 +851,43 @@ func isStringMap(n *Node) bool {
851
851
return true
852
852
}
853
853
854
+ //
855
+
856
+ // StuctMeta can be added to a struct and holds the comments and field ordering for that struct
857
+ type StructMeta interface {
858
+ // Returns the fields in proper order
859
+ GetFieldsIndex () []fieldInfo
860
+
861
+ // Returns the head line and foot comments for the field
862
+ GetComments () [][]comments
863
+ }
864
+
865
+ // structMeta implements the StructMeta interface
866
+ type structMeta struct {
867
+ FieldsIndex []fieldInfo
868
+ Comments [][]comments
869
+ }
870
+
871
+ // comments holds the 3 possible comments for a node.
872
+ type comments struct {
873
+ head []byte
874
+ line []byte
875
+ foot []byte
876
+ }
877
+
878
+ const yamlMeta = "yaml_meta"
879
+
880
+ func (s * structMeta ) GetFieldsIndex () []fieldInfo {
881
+ return s .FieldsIndex
882
+ }
883
+
884
+ func (s * structMeta ) GetComments () [][]comments {
885
+ return s .Comments
886
+ }
887
+
854
888
func (d * decoder ) mappingStruct (n * Node , out reflect.Value ) (good bool ) {
889
+ fieldsIndex := make ([]fieldInfo , 0 )
890
+ com := make ([][]comments , 0 )
855
891
sinfo , err := getStructInfo (out .Type ())
856
892
if err != nil {
857
893
panic (err )
@@ -900,6 +936,21 @@ func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
900
936
field = d .fieldByIndex (n , out , info .Inline )
901
937
}
902
938
d .unmarshal (n .Content [i + 1 ], field )
939
+
940
+ keyComments := comments {
941
+ head : []byte (ni .HeadComment ),
942
+ line : []byte (ni .LineComment ),
943
+ foot : []byte (ni .FootComment ),
944
+ }
945
+ valComments := comments {
946
+ head : []byte (n .Content [i + 1 ].HeadComment ),
947
+ line : []byte (n .Content [i + 1 ].LineComment ),
948
+ foot : []byte (n .Content [i + 1 ].FootComment ),
949
+ }
950
+
951
+ fieldsIndex = append (fieldsIndex , info )
952
+ com = append (com , []comments {keyComments , valComments })
953
+
903
954
} else if sinfo .InlineMap != - 1 {
904
955
if inlineMap .IsNil () {
905
956
inlineMap .Set (reflect .MakeMap (inlineMap .Type ()))
@@ -911,6 +962,18 @@ func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
911
962
d .terrors = append (d .terrors , fmt .Sprintf ("line %d: field %s not found in type %s" , ni .Line , name .String (), out .Type ()))
912
963
}
913
964
}
965
+
966
+ // This change is populating yamlMeta field with the with field order and comments
967
+ if idxInfo , idxOk := sinfo .FieldsMap [yamlMeta ]; idxOk {
968
+ idxField := out .Field (idxInfo .Num )
969
+ fValue := & structMeta {
970
+ FieldsIndex : fieldsIndex ,
971
+ Comments : com ,
972
+ }
973
+
974
+ idxField .Set (reflect .ValueOf (fValue ))
975
+ }
976
+
914
977
return true
915
978
}
916
979
0 commit comments