Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Again cherry pick amino snake case and comments #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading