Skip to content

Commit

Permalink
fix tests; flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Nov 23, 2023
1 parent f6801d5 commit 127ce58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
6 changes: 3 additions & 3 deletions tm2/pkg/amino/genproto/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ option go_package = "github.com/gnolang/gno/tm2/pkg/amino/genproto/pb";
// message comment
message TestMessageName {
// field comment 1
string FieldName1 = 1;
string field_name1 = 1 [json_name = "FieldName1"];
// field comment 2
repeated uint64 FieldName2 = 2;
repeated uint64 field_name2 = 2 [json_name = "FieldName2"];
}
// message comment 2
message TestMessageName2 {
// another field comment
string FieldName = 1;
string field_name = 1 [json_name = "FieldName"];
}`)
}
50 changes: 30 additions & 20 deletions tm2/pkg/amino/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,37 @@ func (pkg *Package) WithComments(filename string) *Package {
}

ast.Inspect(f, func(node ast.Node) bool {
if genDecl, ok := node.(*ast.GenDecl); ok {
for _, spec := range genDecl.Specs {
if typeSpec, ok := spec.(*ast.TypeSpec); ok {
if pkgType := pkg.getTypeByName(typeSpec.Name.Name); pkgType != nil {
if genDecl.Doc != nil {
// Set the type comment.
pkgType.Comment = strings.TrimSpace(genDecl.Doc.Text())
}
if structType, ok := typeSpec.Type.(*ast.StructType); ok {
for _, field := range structType.Fields.List {
if field.Names != nil && len(field.Names) == 1 && field.Doc != nil {
// Set the field comment.
if pkgType.FieldComments == nil {
pkgType.FieldComments = make(map[string]string)
}

pkgType.FieldComments[field.Names[0].Name] = strings.TrimSpace(field.Doc.Text())
}
}
}
genDecl, ok := node.(*ast.GenDecl)
if !ok {
return true
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue

Check warning on line 223 in tm2/pkg/amino/pkg/pkg.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/amino/pkg/pkg.go#L215-L223

Added lines #L215 - L223 were not covered by tests
}

pkgType := pkg.getTypeByName(typeSpec.Name.Name)
if pkgType == nil {
continue

Check warning on line 228 in tm2/pkg/amino/pkg/pkg.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/amino/pkg/pkg.go#L226-L228

Added lines #L226 - L228 were not covered by tests
}
if genDecl.Doc != nil {
// Set the type comment.
pkgType.Comment = strings.TrimSpace(genDecl.Doc.Text())
}

Check warning on line 233 in tm2/pkg/amino/pkg/pkg.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/amino/pkg/pkg.go#L230-L233

Added lines #L230 - L233 were not covered by tests

structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
continue

Check warning on line 237 in tm2/pkg/amino/pkg/pkg.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/amino/pkg/pkg.go#L235-L237

Added lines #L235 - L237 were not covered by tests
}
for _, field := range structType.Fields.List {
if field.Names != nil && len(field.Names) == 1 && field.Doc != nil {
// Set the field comment.
if pkgType.FieldComments == nil {
pkgType.FieldComments = make(map[string]string)
}

Check warning on line 244 in tm2/pkg/amino/pkg/pkg.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/amino/pkg/pkg.go#L239-L244

Added lines #L239 - L244 were not covered by tests

pkgType.FieldComments[field.Names[0].Name] = strings.TrimSpace(field.Doc.Text())

Check warning on line 246 in tm2/pkg/amino/pkg/pkg.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/amino/pkg/pkg.go#L246

Added line #L246 was not covered by tests
}
}
}
Expand Down

0 comments on commit 127ce58

Please sign in to comment.