Skip to content

Commit

Permalink
fix(tm2/std)!: use snake_case JSON fields for MemFile and MemPackage (g…
Browse files Browse the repository at this point in the history
…nolang#2019)

This PR changes the MemFile and MemPackage to use `snake_case` fields
for their JSON key names. This matches what all other objects in
transactions do. A new test ensures that no transaction objects within
the VM keeper have uppercase runes within their exported json fields.

**BREAKING CHANGE:** old `m_addpkg` and `m_run` transactions will stop
working. Clients will need to update accordingly, and this change needs
to be synced like gnolang#1939.

cc/ @zivkovicmilos @gnolang/berty @gnolang/onbloc

---------

Co-authored-by: Miloš Živković <milos.zivkovic@tendermint.com>
  • Loading branch information
2 people authored and gfanton committed Jul 23, 2024
1 parent 3b76414 commit 425bb0f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
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) {
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

0 comments on commit 425bb0f

Please sign in to comment.