From 1ae7a4091b62df30bd3b305de01ec7f838aa015a Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 2 May 2024 13:53:34 +0200 Subject: [PATCH 1/4] feat(tm2/pkg/std)!: use snake_case JSON fields for MemFile and MemPackage --- gno.land/pkg/sdk/vm/package_test.go | 46 +++++++++++++++++++++++++++++ tm2/pkg/std/memfile.go | 10 +++---- 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 gno.land/pkg/sdk/vm/package_test.go diff --git a/gno.land/pkg/sdk/vm/package_test.go b/gno.land/pkg/sdk/vm/package_test.go new file mode 100644 index 00000000000..f3d1532199d --- /dev/null +++ b/gno.land/pkg/sdk/vm/package_test.go @@ -0,0 +1,46 @@ +package vm + +import ( + "reflect" + "strings" + "testing" + "unicode" + + "github.com/stretchr/testify/assert" +) + +func TestJSONSnakeCase(t *testing.T) { + for _, typ := range Package.Types { + assertJSONSnakeCase(t, typ.Type) + } +} + +func assertJSONSnakeCase(t *testing.T, typ reflect.Type) { + 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) + } + } +} diff --git a/tm2/pkg/std/memfile.go b/tm2/pkg/std/memfile.go index 5655876c9bb..2df519bb2d6 100644 --- a/tm2/pkg/std/memfile.go +++ b/tm2/pkg/std/memfile.go @@ -8,8 +8,8 @@ import ( ) type MemFile struct { - Name string - Body string + Name string `json:"name"` + Body string `json:"body"` } // MemPackage represents the information and files of a package which will be @@ -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 { From ad5f833882e1d6bc24ae9c5846a9b0f81363303b Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 2 May 2024 13:55:27 +0200 Subject: [PATCH 2/4] make it a helper function --- gno.land/pkg/sdk/vm/package_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gno.land/pkg/sdk/vm/package_test.go b/gno.land/pkg/sdk/vm/package_test.go index f3d1532199d..946063300db 100644 --- a/gno.land/pkg/sdk/vm/package_test.go +++ b/gno.land/pkg/sdk/vm/package_test.go @@ -16,6 +16,8 @@ func TestJSONSnakeCase(t *testing.T) { } func assertJSONSnakeCase(t *testing.T, typ reflect.Type) { + t.Helper() + switch typ.Kind() { case reflect.Array, reflect.Slice, reflect.Pointer: assertJSONSnakeCase(t, typ.Elem()) From ffe015250795ca022201722ecdc2d4cbebf9f87a Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Wed, 29 May 2024 16:02:07 +0200 Subject: [PATCH 3/4] updates --- gno.land/pkg/sdk/vm/package_test.go | 1 + tm2/pkg/std/memfile.go | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/gno.land/pkg/sdk/vm/package_test.go b/gno.land/pkg/sdk/vm/package_test.go index 946063300db..f19cc55aa20 100644 --- a/gno.land/pkg/sdk/vm/package_test.go +++ b/gno.land/pkg/sdk/vm/package_test.go @@ -10,6 +10,7 @@ import ( ) func TestJSONSnakeCase(t *testing.T) { + t.Parallel() for _, typ := range Package.Types { assertJSONSnakeCase(t, typ.Type) } diff --git a/tm2/pkg/std/memfile.go b/tm2/pkg/std/memfile.go index be45f2c90fb..600108e3316 100644 --- a/tm2/pkg/std/memfile.go +++ b/tm2/pkg/std/memfile.go @@ -8,8 +8,8 @@ import ( ) type MemFile struct { - Name string `json:"name"` - Body string `json:"body"` + Name string `json:"name" yaml:"name"` + Body string `json:"body" yaml:"body"` } // MemPackage represents the information and files of a package which will be From 5b426a52259eb17d45c75e25750542fc2b8a0964 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Tue, 25 Jun 2024 17:27:25 +0200 Subject: [PATCH 4/4] fixup --- gno.land/pkg/sdk/vm/errors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gno.land/pkg/sdk/vm/errors.go b/gno.land/pkg/sdk/vm/errors.go index 132d98b7ecd..0020e989eb6 100644 --- a/gno.land/pkg/sdk/vm/errors.go +++ b/gno.land/pkg/sdk/vm/errors.go @@ -20,7 +20,7 @@ type ( InvalidExprError struct{ abciError } TypeCheckError struct { abciError - Errors []string + Errors []string `json:"errors"` } )