Skip to content

Commit

Permalink
Merge pull request #4 from berty/chore/again-cherry-pick-Amino-snake_…
Browse files Browse the repository at this point in the history
…case-and-comments

Chore: Again cherry pick amino snake case and comments
  • Loading branch information
jefft0 authored Oct 25, 2023
2 parents 81fef3d + 91659e5 commit 23d453e
Show file tree
Hide file tree
Showing 5 changed files with 474 additions and 9 deletions.
61 changes: 61 additions & 0 deletions tm2/pkg/amino/genproto/comments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package genproto

import (
"path"
"testing"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/jaekwon/testify/assert"
)

// message comment
type TestMessageName struct {
// field comment 1
FieldName1 string
// field comment 2
FieldName2 []uint64
}

// message comment 2
type TestMessageName2 struct {
// another field comment
FieldName string
}

func TestComments(t *testing.T) {
pkg := amino.RegisterPackage(
amino.NewPackage(
"github.com/gnolang/gno/tm2/pkg/amino/genproto",
"amino_test",
amino.GetCallersDirname(),
).WithTypes(
&TestMessageName{},
&TestMessageName2{},
// Add comments from this same source file.
).WithComments(path.Join(amino.GetCallersDirname(), "comments_test.go")))

p3c := NewP3Context()
p3c.RegisterPackage(pkg)
p3c.ValidateBasic()
p3doc := p3c.GenerateProto3SchemaForTypes(pkg, pkg.ReflectTypes()...)
proto3Schema := p3doc.Print()
assert.Equal(t, proto3Schema, `syntax = "proto3";
package amino_test;
option go_package = "github.com/gnolang/gno/tm2/pkg/amino/genproto/pb";
// messages
// message comment
message TestMessageName {
// field comment 1
string field_name1 = 1;
// field comment 2
repeated uint64 field_name2 = 2;
}
// message comment 2
message TestMessageName2 {
// another field comment
string field_name = 1;
}`)
}
13 changes: 12 additions & 1 deletion tm2/pkg/amino/genproto/genproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/amino/genproto/stringutil"
"github.com/gnolang/gno/tm2/pkg/amino/pkg"
)

Expand Down Expand Up @@ -191,6 +192,13 @@ func (p3c *P3Context) GenerateProto3MessagePartial(p3doc *P3Doc, rt reflect.Type

p3msg.Name = info.Name // not rinfo.

var fieldComments map[string]string
if pkgType, ok := rinfo.Package.GetType(rt); ok {
p3msg.Comment = pkgType.Comment
// We will check for optional field comments below.
fieldComments = pkgType.FieldComments
}

// Append to p3msg.Fields, fields of the struct.
for _, field := range rsfields { // rinfo.
fp3, fp3IsRepeated, implicit := typeToP3Type(info.Package, field.TypeInfo, field.FieldOptions)
Expand All @@ -206,9 +214,12 @@ func (p3c *P3Context) GenerateProto3MessagePartial(p3doc *P3Doc, rt reflect.Type
p3Field := P3Field{
Repeated: fp3IsRepeated,
Type: fp3,
Name: field.Name,
Name: stringutil.ToLowerSnakeCase(field.Name),
Number: field.FieldOptions.BinFieldNum,
}
if fieldComments != nil {
p3Field.Comment = fieldComments[field.Name]
}
p3msg.Fields = append(p3msg.Fields, p3Field)
}

Expand Down
12 changes: 6 additions & 6 deletions tm2/pkg/amino/genproto/genproto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func TestBasic(t *testing.T) {
obj := sm1.StructSM{}
p3message := p3c.GenerateProto3MessagePartial(&p3doc, reflect.TypeOf(obj))
assert.Equal(t, p3message.Print(), `message StructSM {
sint64 FieldA = 1;
string FieldB = 2;
submodule2.StructSM2 FieldC = 3;
sint64 field_a = 1;
string field_b = 2;
submodule2.StructSM2 field_c = 3;
}
`)

Expand All @@ -38,8 +38,8 @@ import "github.com/gnolang/gno/tm2/pkg/amino/genproto/example/submodule2/submodu
// messages
message StructSM {
sint64 FieldA = 1;
string FieldB = 2;
submodule2.StructSM2 FieldC = 3;
sint64 field_a = 1;
string field_b = 2;
submodule2.StructSM2 field_c = 3;
}`)
}
Loading

0 comments on commit 23d453e

Please sign in to comment.