diff --git a/testdata/cross-package-resource.wit.json.golden b/testdata/cross-package-resource.wit.json.golden index 06d5d7d8..6255326e 100644 --- a/testdata/cross-package-resource.wit.json.golden +++ b/testdata/cross-package-resource.wit.json.golden @@ -6,7 +6,7 @@ TypeDefs: map[string]*wit.TypeDef{ "r": &wit.TypeDef{ Name: &"r", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", diff --git a/testdata/name-both-resource-and-type.wit.json.golden b/testdata/name-both-resource-and-type.wit.json.golden index 462b6481..cadce1dd 100644 --- a/testdata/name-both-resource-and-type.wit.json.golden +++ b/testdata/name-both-resource-and-type.wit.json.golden @@ -6,7 +6,7 @@ TypeDefs: map[string]*wit.TypeDef{ "a": &wit.TypeDef{ Name: &"a", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", diff --git a/testdata/resources-empty.wit.json.golden b/testdata/resources-empty.wit.json.golden index e8f8c5b6..2eb6192b 100644 --- a/testdata/resources-empty.wit.json.golden +++ b/testdata/resources-empty.wit.json.golden @@ -6,7 +6,7 @@ TypeDefs: map[string]*wit.TypeDef{ "r1": &wit.TypeDef{ Name: &"r1", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -16,7 +16,7 @@ Functions: map[string]*wit.Function{ "t1": &wit.Function{ Name: "t1", - Kind: &wit.Freestanding{}, + Kind: &wit.Freestanding{...}, Params: []wit.Param{ wit.Param{ Name: "a", diff --git a/testdata/resources-multiple-returns-borrow.wit.json.golden b/testdata/resources-multiple-returns-borrow.wit.json.golden index 71a3d9c1..6c5e13dc 100644 --- a/testdata/resources-multiple-returns-borrow.wit.json.golden +++ b/testdata/resources-multiple-returns-borrow.wit.json.golden @@ -6,7 +6,7 @@ TypeDefs: map[string]*wit.TypeDef{ "r1": &wit.TypeDef{ Name: &"r1", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -50,7 +50,7 @@ }, "t1": &wit.Function{ Name: "t1", - Kind: &wit.Freestanding{}, + Kind: &wit.Freestanding{...}, Params: []wit.Param{ wit.Param{ Name: "a", diff --git a/testdata/resources-multiple-returns-own.wit.json.golden b/testdata/resources-multiple-returns-own.wit.json.golden index df865a80..f58f9baf 100644 --- a/testdata/resources-multiple-returns-own.wit.json.golden +++ b/testdata/resources-multiple-returns-own.wit.json.golden @@ -6,7 +6,7 @@ TypeDefs: map[string]*wit.TypeDef{ "r1": &wit.TypeDef{ Name: &"r1", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -59,7 +59,7 @@ }, "t1": &wit.Function{ Name: "t1", - Kind: &wit.Freestanding{}, + Kind: &wit.Freestanding{...}, Params: []wit.Param{ wit.Param{ Name: "a", diff --git a/testdata/resources-multiple.wit.json.golden b/testdata/resources-multiple.wit.json.golden index 5a358516..706f4d2f 100644 --- a/testdata/resources-multiple.wit.json.golden +++ b/testdata/resources-multiple.wit.json.golden @@ -6,7 +6,7 @@ TypeDefs: map[string]*wit.TypeDef{ "r1": &wit.TypeDef{ Name: &"r1", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -270,7 +270,7 @@ }, "t1": &wit.Function{ Name: "t1", - Kind: &wit.Freestanding{}, + Kind: &wit.Freestanding{...}, Params: []wit.Param{ wit.Param{ Name: "a", diff --git a/testdata/resources-return-borrow.wit.json.golden b/testdata/resources-return-borrow.wit.json.golden index 99229df0..4a4d380c 100644 --- a/testdata/resources-return-borrow.wit.json.golden +++ b/testdata/resources-return-borrow.wit.json.golden @@ -6,7 +6,7 @@ TypeDefs: map[string]*wit.TypeDef{ "r1": &wit.TypeDef{ Name: &"r1", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -46,7 +46,7 @@ }, "t1": &wit.Function{ Name: "t1", - Kind: &wit.Freestanding{}, + Kind: &wit.Freestanding{...}, Params: []wit.Param{ wit.Param{ Name: "a", diff --git a/testdata/resources-return-own.wit.json.golden b/testdata/resources-return-own.wit.json.golden index 9f3bf5f9..ed2f3192 100644 --- a/testdata/resources-return-own.wit.json.golden +++ b/testdata/resources-return-own.wit.json.golden @@ -6,7 +6,7 @@ TypeDefs: map[string]*wit.TypeDef{ "r1": &wit.TypeDef{ Name: &"r1", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -55,7 +55,7 @@ }, "t1": &wit.Function{ Name: "t1", - Kind: &wit.Freestanding{}, + Kind: &wit.Freestanding{...}, Params: []wit.Param{ wit.Param{ Name: "a", diff --git a/testdata/resources.wit.json.golden b/testdata/resources.wit.json.golden index cfeaeb20..b5ea5880 100644 --- a/testdata/resources.wit.json.golden +++ b/testdata/resources.wit.json.golden @@ -8,7 +8,7 @@ Kind: &wit.Constructor{ Type: &wit.TypeDef{ Name: &"c", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.World{...}, Docs: wit.Docs{ Contents: "", @@ -37,7 +37,7 @@ }, "a": &wit.TypeDef{ Name: &"a", - Kind: nil, + Kind: &wit.Resource{...}, Owner: &wit.World{...}, Docs: wit.Docs{ Contents: "", @@ -45,7 +45,7 @@ }, "b": &wit.TypeDef{ Name: &"b", - Kind: nil, + Kind: &wit.Resource{...}, Owner: &wit.World{...}, Docs: wit.Docs{ Contents: "", @@ -66,7 +66,7 @@ TypeDefs: map[string]*wit.TypeDef{ "a": &wit.TypeDef{ Name: &"a", - Kind: nil, + Kind: &wit.Resource{...}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -74,7 +74,7 @@ }, "b": &wit.TypeDef{ Name: &"b", - Kind: nil, + Kind: &wit.Resource{...}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -82,7 +82,7 @@ }, "c": &wit.TypeDef{ Name: &"c", - Kind: nil, + Kind: &wit.Resource{...}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -90,7 +90,7 @@ }, "d": &wit.TypeDef{ Name: &"d", - Kind: nil, + Kind: &wit.Resource{...}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -98,7 +98,7 @@ }, "e": &wit.TypeDef{ Name: &"e", - Kind: nil, + Kind: &wit.Resource{...}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -308,7 +308,7 @@ TypeDefs: map[string]*wit.TypeDef{ "a": &wit.TypeDef{ Name: &"a", - Kind: nil, + Kind: &wit.Resource{...}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", diff --git a/testdata/resources1.wit.json.golden b/testdata/resources1.wit.json.golden index 54f76fb8..e3946876 100644 --- a/testdata/resources1.wit.json.golden +++ b/testdata/resources1.wit.json.golden @@ -6,7 +6,7 @@ TypeDefs: map[string]*wit.TypeDef{ "r1": &wit.TypeDef{ Name: &"r1", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -41,7 +41,7 @@ }, "t1": &wit.Function{ Name: "t1", - Kind: &wit.Freestanding{}, + Kind: &wit.Freestanding{...}, Params: []wit.Param{ wit.Param{ Name: "a", diff --git a/testdata/world-top-level-resources.wit.json.golden b/testdata/world-top-level-resources.wit.json.golden index 1d577f96..4a8405ee 100644 --- a/testdata/world-top-level-resources.wit.json.golden +++ b/testdata/world-top-level-resources.wit.json.golden @@ -8,7 +8,7 @@ TypeDefs: map[string]*wit.TypeDef{ "request": &wit.TypeDef{ Name: &"request", - Kind: nil, + Kind: &wit.Resource{}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -16,7 +16,7 @@ }, "response": &wit.TypeDef{ Name: &"response", - Kind: nil, + Kind: &wit.Resource{...}, Owner: &wit.Interface{...}, Docs: wit.Docs{ Contents: "", @@ -154,7 +154,7 @@ Functions: map[string]*wit.Function{ "handle": &wit.Function{ Name: "handle", - Kind: &wit.Freestanding{}, + Kind: &wit.Freestanding{...}, Params: []wit.Param{ wit.Param{ Name: "some", diff --git a/wit/codec.go b/wit/codec.go index 26c71a27..6ab9d82d 100644 --- a/wit/codec.go +++ b/wit/codec.go @@ -258,13 +258,21 @@ type typeDefKindCodec struct { v *TypeDefKind } +func (c *typeDefKindCodec) DecodeString(s string) error { + switch s { + case "resource": + *c.v = &Resource{} + } + return nil +} + func (c *typeDefKindCodec) DecodeField(dec codec.Decoder, name string) error { var err error switch name { case "record": v := &Record{} *c.v, err = v, dec.Decode(v) - case "resource": + case "resource": // TODO: this might not be necessary v := &Resource{} *c.v, err = v, dec.Decode(v) case "handle": diff --git a/wit/golden_test.go b/wit/golden_test.go index 88a576b7..146c8e47 100644 --- a/wit/golden_test.go +++ b/wit/golden_test.go @@ -3,6 +3,7 @@ package wit import ( "flag" "os" + "strings" "testing" "github.com/k0kubun/pp/v3" @@ -17,7 +18,7 @@ func TestGoldenFiles(t *testing.T) { p.SetColoringEnabled(false) err := loadTestdata(func(path string, res *Resolve) error { - t.Run(path, func(t *testing.T) { + t.Run(strings.TrimPrefix(path, testdataDir), func(t *testing.T) { data := p.Sprint(res) compareOrWrite(t, path, data) }) diff --git a/wit/resolve.go b/wit/resolve.go index 3c746fa3..b1ea8ec8 100644 --- a/wit/resolve.go +++ b/wit/resolve.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strings" + "unsafe" "github.com/coreos/go-semver/semver" )