@@ -358,34 +358,22 @@ func fullTypeName(pkgName, typeName string) string {
358
358
return typeName
359
359
}
360
360
361
- func (parser * Parser ) getTypeSchema (typeName string , file * ast.File , field * ast. Field , parentSchema * TypeSchema ) (* TypeSchema , error ) {
361
+ func (parser * Parser ) getTypeSchema (typeName string , file * ast.File , parentSchema * TypeSchema ) (* TypeSchema , error ) {
362
362
if IsGolangPrimitiveType (typeName ) {
363
- if field != nil {
364
- name := field .Names [0 ].Name
365
- return & TypeSchema {
366
- Name : name ,
367
- FullName : name ,
368
- Type : typeName ,
369
- Comment : strings .TrimSuffix (field .Comment .Text (), "\n " ),
370
- Parent : parentSchema ,
371
- TagValue : getAllTagValue (field ),
372
- }, nil
373
- } else {
374
- return & TypeSchema {
375
- Name : "" ,
376
- FullName : typeName ,
377
- Type : typeName ,
378
- Parent : parentSchema ,
379
- }, nil
380
- }
363
+ return & TypeSchema { //root type
364
+ Name : typeName ,
365
+ FullName : typeName ,
366
+ Type : typeName ,
367
+ Parent : parentSchema ,
368
+ }, nil
381
369
}
382
370
383
371
typeSpecDef := parser .packages .FindTypeSpec (typeName , file , true )
384
372
if typeSpecDef == nil {
385
373
return nil , fmt .Errorf ("cannot find type definition: %s" , typeName )
386
374
}
387
375
388
- schema , err := parser .ParseDefinition (typeSpecDef , field , parentSchema )
376
+ schema , err := parser .ParseDefinition (typeSpecDef , parentSchema )
389
377
if err != nil {
390
378
return nil , err
391
379
}
@@ -394,45 +382,32 @@ func (parser *Parser) getTypeSchema(typeName string, file *ast.File, field *ast.
394
382
395
383
// ParseDefinition parses given type spec that corresponds to the type under
396
384
// given name and package
397
- func (parser * Parser ) ParseDefinition (typeSpecDef * TypeSpecDef , field * ast. Field , parentSchema * TypeSchema ) (* TypeSchema , error ) {
385
+ func (parser * Parser ) ParseDefinition (typeSpecDef * TypeSpecDef , parentSchema * TypeSchema ) (* TypeSchema , error ) {
398
386
typeName := typeSpecDef .FullName ()
399
-
400
387
if parentSchema != nil && parentSchema .isInTypeChain (typeSpecDef ) {
401
388
fmt .Printf ("Skipping '%s', recursion detected.\n " , typeName )
402
389
return & TypeSchema {
403
- Name : typeName ,
390
+ Name : typeSpecDef . Name () ,
404
391
FullName : typeName ,
405
392
Type : OBJECT ,
406
393
PkgPath : typeSpecDef .PkgPath ,
407
394
Parent : parentSchema ,
408
- TagValue : getAllTagValue (field ),
395
+ Comment : fmt .Sprintf ("%s(Recursion...)" , strings .TrimSuffix (typeSpecDef .TypeSpec .Comment .Text (), "\n " )),
396
+ // TagValue: getAllTagValue(field),
409
397
}, nil
410
398
}
411
399
412
400
fmt .Printf ("Generating %s\n " , typeName )
413
401
414
- // return parser.parseTypeExpr(typeSpecDef.File, field, typeSpecDef.TypeSpec.Type, parentSchema)
415
-
416
402
switch expr := typeSpecDef .TypeSpec .Type .(type ) {
417
403
// type Foo struct {...}
418
404
case * ast.StructType :
419
- schema , err := parser .parseStruct (typeSpecDef , typeSpecDef .File , expr .Fields , parentSchema )
420
- if err != nil {
421
- return nil , err
422
- }
423
- if field != nil {
424
- if field .Names != nil {
425
- schema .Name = field .Names [0 ].Name
426
- }
427
- schema .TagValue = getAllTagValue (field )
428
- schema .Comment = strings .TrimSuffix (field .Comment .Text (), "\n " )
429
- }
430
- return schema , err
405
+ return parser .parseStruct (typeSpecDef , typeSpecDef .File , expr .Fields , parentSchema )
431
406
case * ast.Ident :
432
- return parser .getTypeSchema (expr .Name , typeSpecDef .File , field , parentSchema )
407
+ return parser .getTypeSchema (expr .Name , typeSpecDef .File , parentSchema )
433
408
case * ast.SelectorExpr :
434
409
if xIdent , ok := expr .X .(* ast.Ident ); ok {
435
- return parser .getTypeSchema (fullTypeName (xIdent .Name , expr .Sel .Name ), typeSpecDef .File , field , parentSchema )
410
+ return parser .getTypeSchema (fullTypeName (xIdent .Name , expr .Sel .Name ), typeSpecDef .File , parentSchema )
436
411
}
437
412
case * ast.MapType :
438
413
if keyIdent , ok := expr .Key .(* ast.Ident ); ok {
@@ -443,7 +418,7 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef, field *ast.Field
443
418
Properties : map [string ]* TypeSchema {},
444
419
Parent : parentSchema ,
445
420
}
446
- schema , err := parser .parseTypeExpr (typeSpecDef .File , field , expr .Value , mapSchema )
421
+ schema , err := parser .parseTypeExpr (typeSpecDef .File , expr .Value , mapSchema )
447
422
if err != nil {
448
423
return nil , err
449
424
}
@@ -471,51 +446,32 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef, field *ast.Field
471
446
return & sch , nil
472
447
}
473
448
474
- func (parser * Parser ) parseTypeExpr (file * ast.File , field * ast. Field , typeExpr ast.Expr , parentSchema * TypeSchema ) (* TypeSchema , error ) {
449
+ func (parser * Parser ) parseTypeExpr (file * ast.File , typeExpr ast.Expr , parentSchema * TypeSchema ) (* TypeSchema , error ) {
475
450
switch expr := typeExpr .(type ) {
476
451
// type Foo interface{}
477
452
case * ast.InterfaceType :
478
453
return & TypeSchema {
479
- Name : field . Names [ 0 ]. Name ,
454
+ Name : "" ,
480
455
Type : ANY ,
481
456
FullName : ANY ,
482
457
Parent : parentSchema ,
483
- TagValue : getAllTagValue (field ),
484
458
}, nil
485
459
486
- // type Foo struct {...}
487
- case * ast.StructType :
488
- schema , err := parser .parseStruct (nil , file , expr .Fields , parentSchema )
489
- if err != nil {
490
- return nil , err
491
- }
492
- schema .Name = field .Names [0 ].Name
493
- schema .TagValue = getAllTagValue (field )
494
- return schema , err
495
-
496
460
// type Foo Baz
497
461
case * ast.Ident :
498
- schema , err := parser .getTypeSchema (expr .Name , file , field , parentSchema )
499
- if err != nil {
500
- return nil , err
501
- }
502
- if field != nil && field .Names != nil {
503
- schema .Name = field .Names [0 ].Name
504
- schema .TagValue = getAllTagValue (field )
505
- }
506
- return schema , err
462
+ return parser .getTypeSchema (expr .Name , file , parentSchema )
507
463
// type Foo *Baz
508
464
case * ast.StarExpr :
509
- return parser .parseTypeExpr (file , field , expr .X , parentSchema )
465
+ return parser .parseTypeExpr (file , expr .X , parentSchema )
510
466
511
467
// type Foo pkg.Bar
512
468
case * ast.SelectorExpr :
513
469
if xIdent , ok := expr .X .(* ast.Ident ); ok {
514
- return parser .getTypeSchema (fullTypeName (xIdent .Name , expr .Sel .Name ), file , field , parentSchema )
470
+ return parser .getTypeSchema (fullTypeName (xIdent .Name , expr .Sel .Name ), file , parentSchema )
515
471
}
516
472
// type Foo []Baz
517
473
case * ast.ArrayType :
518
- itemSchema , err := parser .parseTypeExpr (file , field , expr .Elt , parentSchema )
474
+ itemSchema , err := parser .parseTypeExpr (file , expr .Elt , parentSchema )
519
475
if err != nil {
520
476
return nil , err
521
477
}
@@ -530,7 +486,7 @@ func (parser *Parser) parseTypeExpr(file *ast.File, field *ast.Field, typeExpr a
530
486
Properties : map [string ]* TypeSchema {},
531
487
Parent : parentSchema ,
532
488
}
533
- schema , err := parser .parseTypeExpr (file , field , expr .Value , mapSchema )
489
+ schema , err := parser .parseTypeExpr (file , expr .Value , mapSchema )
534
490
if err != nil {
535
491
return nil , err
536
492
}
@@ -542,11 +498,12 @@ func (parser *Parser) parseTypeExpr(file *ast.File, field *ast.Field, typeExpr a
542
498
schema .TagValue = ""
543
499
mapSchema .Properties [example ] = schema
544
500
return mapSchema , nil
501
+ } else {
502
+ return nil , fmt .Errorf ("error: map key type %s, just support string or int" , keyIdent .Name )
545
503
}
546
504
}
547
-
548
- // case *ast.FuncType:
549
- // return nil, ErrFuncTypeField
505
+ case * ast.FuncType :
506
+ return nil , errors .New ("filed type can't be func" )
550
507
// ...
551
508
default :
552
509
fmt .Printf ("Type definition of type '%T' is not supported yet. Using 'object' instead.\n " , typeExpr )
@@ -569,13 +526,26 @@ func (parser *Parser) parseStruct(typeSpecDef *TypeSpecDef, file *ast.File, fiel
569
526
}
570
527
571
528
for _ , field := range fields .List {
572
- schema , err := parser .parseStructField (file , field , structSchema )
529
+ if field .Names != nil {
530
+ name := field .Names [0 ].Name
531
+ if ! ast .IsExported (name ) {
532
+ continue
533
+ }
534
+ }
535
+ schema , err := parser .parseTypeExpr (file , field .Type , structSchema )
573
536
if err != nil {
574
537
return nil , err
575
538
}
576
539
if schema == nil {
577
540
continue
578
541
}
542
+ if field != nil {
543
+ if field .Names != nil {
544
+ schema .Name = field .Names [0 ].Name
545
+ }
546
+ schema .TagValue = getAllTagValue (field )
547
+ schema .Comment = strings .TrimSuffix (field .Comment .Text (), "\n " )
548
+ }
579
549
if field .Names == nil { //nested struct, replace with child properties
580
550
for _ , p := range schema .Properties {
581
551
if _ , ok := structSchema .Properties [strings .ToLower (p .Name )]; ! ok { //if not exists key
@@ -588,13 +558,3 @@ func (parser *Parser) parseStruct(typeSpecDef *TypeSpecDef, file *ast.File, fiel
588
558
}
589
559
return structSchema , nil
590
560
}
591
-
592
- func (parser * Parser ) parseStructField (file * ast.File , field * ast.Field , parentSchama * TypeSchema ) (* TypeSchema , error ) {
593
- if field .Names != nil {
594
- name := field .Names [0 ].Name
595
- if ! ast .IsExported (name ) {
596
- return nil , nil
597
- }
598
- }
599
- return parser .parseTypeExpr (file , field , field .Type , parentSchama )
600
- }
0 commit comments