Skip to content

Commit 76883ef

Browse files
meblumtwpayne
authored andcommitted
fix: order compact generated types
1 parent 997e006 commit 76883ef

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

element.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,17 @@ func (e *element) writeGoType(w io.Writer, options *generateOptions, indentPrefi
198198
case aExportedName < bExportedName:
199199
return -1
200200
case aExportedName == bExportedName:
201-
return 0
201+
// When exported names are equal, fall back to original XML element name
202+
aOriginalName := options.exportNameFunc(a.name)
203+
bOriginalName := options.exportNameFunc(b.name)
204+
switch {
205+
case aOriginalName < bOriginalName:
206+
return -1
207+
case aOriginalName == bOriginalName:
208+
return 0
209+
default:
210+
return 1
211+
}
202212
default:
203213
return 1
204214
}

generator_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -854,12 +854,12 @@ func TestGenerator(t *testing.T) {
854854
),
855855
expectedStr: joinLines(
856856
"type Root struct {",
857-
"\tSingle struct {",
858-
"\t\tValue string `xml:\"value\"`",
859-
"\t} `xml:\"single\"`",
860857
"\tMulti []struct {",
861858
"\t\tValue string `xml:\"value\"`",
862859
"\t} `xml:\"multi\"`",
860+
"\tSingle struct {",
861+
"\t\tValue string `xml:\"value\"`",
862+
"\t} `xml:\"single\"`",
863863
"}",
864864
),
865865
},
@@ -891,8 +891,8 @@ func TestGenerator(t *testing.T) {
891891
"}",
892892
"",
893893
"type Root struct {",
894-
"\tSingle Single `xml:\"single\"`", //nolint:dupword
895894
"\tMulti []Multi `xml:\"multi\"`",
895+
"\tSingle Single `xml:\"single\"`", //nolint:dupword
896896
"}",
897897
"",
898898
"type Single struct {",

0 commit comments

Comments
 (0)