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

fix(tm2/std)!: use snake_case JSON fields for MemFile and MemPackage #2019

Merged
merged 7 commits into from
Jun 26, 2024
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
2 changes: 1 addition & 1 deletion gno.land/pkg/sdk/vm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type (
InvalidExprError struct{ abciError }
TypeCheckError struct {
abciError
Errors []string
Errors []string `json:"errors"`
}
)

Expand Down
49 changes: 49 additions & 0 deletions gno.land/pkg/sdk/vm/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package vm

import (
"reflect"
"strings"
"testing"
"unicode"

"github.com/stretchr/testify/assert"
)

func TestJSONSnakeCase(t *testing.T) {
thehowl marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()
for _, typ := range Package.Types {
assertJSONSnakeCase(t, typ.Type)
}
}

func assertJSONSnakeCase(t *testing.T, typ reflect.Type) {
t.Helper()

switch typ.Kind() {
case reflect.Array, reflect.Slice, reflect.Pointer:
assertJSONSnakeCase(t, typ.Elem())
case reflect.Map:
assertJSONSnakeCase(t, typ.Key())
assertJSONSnakeCase(t, typ.Elem())
case reflect.Struct:
for i := 0; i < typ.NumField(); i++ {
fld := typ.Field(i)
if !fld.IsExported() {
continue
}
jt := fld.Tag.Get("json")
if jt == "" {
if fld.Anonymous {
assertJSONSnakeCase(t, fld.Type)
continue
}
t.Errorf("field %s.%s does not have a json tag but is exported", typ.Name(), fld.Name)
continue
}
has := strings.ContainsFunc(jt, unicode.IsUpper)
assert.False(t, has,
"field %s.%s contains uppercase symbols in json tag", typ.Name(), fld.Name)
assertJSONSnakeCase(t, fld.Type)
}
}
}
10 changes: 5 additions & 5 deletions tm2/pkg/std/memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

type MemFile struct {
Name string
Body string
Name string `json:"name" yaml:"name"`
Body string `json:"body" yaml:"body"`
}

// MemPackage represents the information and files of a package which will be
Expand All @@ -19,9 +19,9 @@ type MemFile struct {
// NOTE: in the future, a MemPackage may represent
// updates/additional-files for an existing package.
type MemPackage struct {
Name string // package name as declared by `package`
Path string // import path
Files []*MemFile
Name string `json:"name" yaml:"name"` // package name as declared by `package`
Path string `json:"path" yaml:"path"` // import path
Files []*MemFile `json:"files" yaml:"files"`
}

func (mempkg *MemPackage) GetFile(name string) *MemFile {
Expand Down
Loading