@@ -12,10 +12,12 @@ import (
1212 "github.com/fatih/structtag"
1313)
1414
15- var validJSNameRegexp = regexp .MustCompile (`(?m)^[\pL_][\pL\pN_]*$` )
16- var backquoteEscapeRegexp = regexp .MustCompile (`([$\\])` )
17- var octalPrefixRegexp = regexp .MustCompile (`^0[0-7]` )
18- var unicode8Regexp = regexp .MustCompile (`\\\\|\\U[\da-fA-F]{8}` )
15+ var (
16+ validJSNameRegexp = regexp .MustCompile (`(?m)^[\pL_][\pL\pN_]*$` )
17+ backquoteEscapeRegexp = regexp .MustCompile (`([$\\])` )
18+ octalPrefixRegexp = regexp .MustCompile (`^0[0-7]` )
19+ unicode8Regexp = regexp .MustCompile (`\\\\|\\U[\da-fA-F]{8}` )
20+ )
1921
2022// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_precedence#table
2123var jsNumberOperatorPrecedence = map [token.Token ]int {
@@ -292,110 +294,115 @@ func (g *PackageGenerator) writeStructFields(s *strings.Builder, fields []*ast.F
292294 required := false
293295 readonly := false
294296
295- var fieldName string
297+ fieldNames := make ([] string , 0 , len ( f . Names ))
296298 if len (f .Names ) == 0 { // anonymous field
297299 if name , valid := getAnonymousFieldName (f .Type ); valid {
298- fieldName = name
300+ fieldNames = append (fieldNames , name )
301+ }
302+ } else {
303+ for _ , name := range f .Names {
304+ if len (name .Name ) == 0 || 'A' > name .Name [0 ] || name .Name [0 ] > 'Z' {
305+ continue
306+ }
307+ fieldNames = append (fieldNames , name .Name )
299308 }
300- }
301- if len (f .Names ) != 0 && f .Names [0 ] != nil && len (f .Names [0 ].Name ) != 0 {
302- fieldName = f .Names [0 ].Name
303- }
304- if len (fieldName ) == 0 || 'A' > fieldName [0 ] || fieldName [0 ] > 'Z' {
305- continue
306309 }
307310
308- var name string
309- var tstype string
310- if f .Tag != nil {
311- tags , err := structtag .Parse (f .Tag .Value [1 : len (f .Tag .Value )- 1 ])
312- if err != nil {
313- panic (err )
314- }
311+ for _ , fieldName := range fieldNames {
315312
316- jsonTag , err := tags .Get ("json" )
317- if err == nil {
318- name = jsonTag .Name
319- if name == "-" {
320- continue
313+ var name string
314+ var tstype string
315+ if f .Tag != nil {
316+ tags , err := structtag .Parse (f .Tag .Value [1 : len (f .Tag .Value )- 1 ])
317+ if err != nil {
318+ panic (err )
321319 }
322320
323- optional = jsonTag .HasOption ("omitempty" ) || jsonTag .HasOption ("omitzero" )
324- }
325- yamlTag , err := tags .Get ("yaml" )
326- if err == nil {
327- name = yamlTag .Name
328- if name == "-" {
329- continue
321+ jsonTag , err := tags .Get ("json" )
322+ if err == nil {
323+ name = jsonTag .Name
324+ if name == "-" {
325+ continue
326+ }
327+
328+ optional = jsonTag .HasOption ("omitempty" ) || jsonTag .HasOption ("omitzero" )
329+ }
330+ yamlTag , err := tags .Get ("yaml" )
331+ if err == nil {
332+ name = yamlTag .Name
333+ if name == "-" {
334+ continue
335+ }
336+
337+ optional = yamlTag .HasOption ("omitempty" )
330338 }
331339
332- optional = yamlTag .HasOption ("omitempty" )
340+ tstypeTag , err := tags .Get ("tstype" )
341+ if err == nil {
342+ tstype = tstypeTag .Name
343+ if tstype == "-" || tstypeTag .HasOption ("extends" ) {
344+ continue
345+ }
346+ required = tstypeTag .HasOption ("required" )
347+ readonly = tstypeTag .HasOption ("readonly" )
348+ }
333349 }
334350
335- tstypeTag , err := tags . Get ( "tstype" )
336- if err == nil {
337- tstype = tstypeTag . Name
338- if tstype == "-" || tstypeTag . HasOption ( "extends" ) {
339- continue
351+ if len ( name ) == 0 {
352+ if g . conf . Flavor == "yaml" {
353+ name = strings . ToLower ( fieldName )
354+ } else {
355+ name = fieldName
340356 }
341- required = tstypeTag .HasOption ("required" )
342- readonly = tstypeTag .HasOption ("readonly" )
343357 }
344- }
345358
346- if len (name ) == 0 {
347- if g .conf .Flavor == "yaml" {
348- name = strings .ToLower (fieldName )
349- } else {
350- name = fieldName
359+ if g .PreserveTypeComments () {
360+ g .writeCommentGroupIfNotNil (s , f .Doc , depth + 1 )
351361 }
352- }
353362
354- if g .PreserveTypeComments () {
355- g .writeCommentGroupIfNotNil (s , f .Doc , depth + 1 )
356- }
363+ g .writeIndent (s , depth + 1 )
364+ quoted := ! validJSName (name )
365+ if quoted {
366+ s .WriteByte ('\'' )
367+ }
368+ if readonly {
369+ s .WriteString ("readonly " )
370+ }
371+ s .WriteString (name )
372+ if quoted {
373+ s .WriteByte ('\'' )
374+ }
357375
358- g .writeIndent (s , depth + 1 )
359- quoted := ! validJSName (name )
360- if quoted {
361- s .WriteByte ('\'' )
362- }
363- if readonly {
364- s .WriteString ("readonly " )
365- }
366- s .WriteString (name )
367- if quoted {
368- s .WriteByte ('\'' )
369- }
376+ switch t := f .Type .(type ) {
377+ case * ast.StarExpr :
378+ optional = ! required
379+ f .Type = t .X
380+ }
370381
371- switch t := f .Type .(type ) {
372- case * ast.StarExpr :
373- optional = ! required
374- f .Type = t .X
375- }
382+ if optional && g .conf .OptionalType == "undefined" {
383+ s .WriteByte ('?' )
384+ }
376385
377- if optional && g .conf .OptionalType == "undefined" {
378- s .WriteByte ('?' )
379- }
386+ s .WriteString (": " )
380387
381- s .WriteString (": " )
388+ if tstype == "" {
389+ g .writeType (s , f .Type , nil , depth , false )
390+ if optional && g .conf .OptionalType == "null" {
391+ s .WriteString (" | null" )
392+ }
393+ } else {
394+ s .WriteString (tstype )
395+ }
396+ s .WriteByte (';' )
382397
383- if tstype == "" {
384- g .writeType (s , f .Type , nil , depth , false )
385- if optional && g .conf .OptionalType == "null" {
386- s .WriteString (" | null" )
398+ if f .Comment != nil && g .PreserveTypeComments () {
399+ // Line comment is present, that means a comment after the field.
400+ s .WriteString (" // " )
401+ s .WriteString (f .Comment .Text ())
402+ } else {
403+ s .WriteByte ('\n' )
387404 }
388- } else {
389- s .WriteString (tstype )
390- }
391- s .WriteByte (';' )
392405
393- if f .Comment != nil && g .PreserveTypeComments () {
394- // Line comment is present, that means a comment after the field.
395- s .WriteString (" // " )
396- s .WriteString (f .Comment .Text ())
397- } else {
398- s .WriteByte ('\n' )
399406 }
400407
401408 }
0 commit comments