Skip to content

Commit

Permalink
rewrite interface{} to any
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Jan 16, 2024
1 parent 017ccee commit a1c4b99
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 156 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -206,7 +206,7 @@ func init() {
gltf.RegisterExtension(ExtensionName, Unmarshal)
}

func Unmarshal(data []byte) (interface{}, error) {
func Unmarshal(data []byte) (any, error) {
foo := new(Foo)
err := json.Unmarshal(data, foo)
return foo, err
Expand Down
32 changes: 16 additions & 16 deletions binary/binary.go
Expand Up @@ -9,22 +9,22 @@
// they all follow the same `generic` interface, and the only difference is the
// type of the value:
//
// type Component interface {
// Scalar([]byte) interface{}
// Vec2([]byte) [2]interface{}
// Vec3([]byte) [2]interface{}
// Vec4([]byte) [2]interface{}
// Mat2([]byte) [2][2]interface{}
// Mat3([]byte) [3][3]interface{}
// Mat4([]byte) [4][4]interface{}
// PutScalar([]byte, interface{})
// PutVec2([]byte, [2]interface{})
// PutVec3([]byte, [3]interface{})
// PutVec4([]byte, [4]interface{})
// PutMat2([]byte, [2][2]interface{})
// PutMat3([]byte, [3][3]interface{})
// PutMat4([]byte, [4][4]interface{})
// }
// type Component interface {
// Scalar([]byte) any
// Vec2([]byte) [2]any
// Vec3([]byte) [2]any
// Vec4([]byte) [2]any
// Mat2([]byte) [2][2]any
// Mat3([]byte) [3][3]any
// Mat4([]byte) [4][4]any
// PutScalar([]byte, any)
// PutVec2([]byte, [2]any)
// PutVec3([]byte, [3]any)
// PutVec4([]byte, [4]any)
// PutMat2([]byte, [2][2]any)
// PutMat3([]byte, [3][3]any)
// PutMat4([]byte, [4][4]any)
// }
//
// This package favors simplicity and compliance over efficiency,
// but it is still an order of magnitude more performant than using the built-in
Expand Down
4 changes: 2 additions & 2 deletions binary/encode.go
Expand Up @@ -14,7 +14,7 @@ import (
//
// Data should be a slice of glTF predefined fixed-size types.
// If data length is greater than the length of b, Read returns io.ErrShortBuffer.
func Read(b []byte, byteStride uint32, data interface{}) error {
func Read(b []byte, byteStride uint32, data any) error {
c, t, n := Type(data)
size := gltf.SizeOfElement(c, t)
if byteStride == 0 {
Expand Down Expand Up @@ -222,7 +222,7 @@ func Read(b []byte, byteStride uint32, data interface{}) error {
//
// Data must be a slice of glTF predefined fixed-size types,
// else it fallbacks to `encoding/binary.Write`.
func Write(b []byte, stride uint32, data interface{}) error {
func Write(b []byte, stride uint32, data any) error {
c, t, n := Type(data)
if n == 0 {
return binary.Write(bytes.NewBuffer(b), binary.LittleEndian, data)
Expand Down
6 changes: 3 additions & 3 deletions binary/encode_test.go
Expand Up @@ -49,12 +49,12 @@ func buildBufferF(n int) []byte {
func TestRead(t *testing.T) {
type args struct {
b []byte
data interface{}
data any
}
tests := []struct {
name string
args args
want interface{}
want any
wantErr bool
}{
{"small", args{[]byte{0, 0}, []int8{1, 2, 3}}, []int8{0, 0}, true},
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestRead(t *testing.T) {
func TestWrite(t *testing.T) {
type args struct {
n int
data interface{}
data any
}
tests := []struct {
name string
Expand Down
6 changes: 3 additions & 3 deletions binary/slice.go
Expand Up @@ -11,7 +11,7 @@ import (
// MakeSliceBuffer returns the slice type associated with c and t and with the given element count.
// If the buffer is an slice which type matches with the expected by the acr then it will
// be used as backing slice.
func MakeSliceBuffer(c gltf.ComponentType, t gltf.AccessorType, count uint32, buffer interface{}) interface{} {
func MakeSliceBuffer(c gltf.ComponentType, t gltf.AccessorType, count uint32, buffer any) any {
if buffer == nil {
return MakeSlice(c, t, count)
}
Expand All @@ -32,7 +32,7 @@ func MakeSliceBuffer(c gltf.ComponentType, t gltf.AccessorType, count uint32, bu
// MakeSlice returns the slice type associated with c and t and with the given element count.
// For example, if c is gltf.ComponentFloat and t is gltf.AccessorVec3
// then MakeSlice(c, t, 5) is equivalent to make([][3]float32, 5).
func MakeSlice(c gltf.ComponentType, t gltf.AccessorType, count uint32) interface{} {
func MakeSlice(c gltf.ComponentType, t gltf.AccessorType, count uint32) any {
var tp reflect.Type
switch c {
case gltf.ComponentUbyte:
Expand Down Expand Up @@ -68,7 +68,7 @@ func MakeSlice(c gltf.ComponentType, t gltf.AccessorType, count uint32) interfac

// Type returns the associated glTF type data.
// It panics if data is not an slice.
func Type(data interface{}) (c gltf.ComponentType, t gltf.AccessorType, count uint32) {
func Type(data any) (c gltf.ComponentType, t gltf.AccessorType, count uint32) {
v := reflect.ValueOf(data)
if v.Kind() != reflect.Slice {
panic(fmt.Sprintf("go3mf: binary.Type expecting a slice but got %s", v.Kind()))
Expand Down
6 changes: 3 additions & 3 deletions binary/slice_test.go
Expand Up @@ -16,7 +16,7 @@ func TestMakeSlice(t *testing.T) {
tests := []struct {
name string
args args
want interface{}
want any
}{
// Scalar
{"[]uint8", args{gltf.ComponentUbyte, gltf.AccessorScalar, 5}, make([]uint8, 5)},
Expand Down Expand Up @@ -82,12 +82,12 @@ func TestMakeSliceBuffer(t *testing.T) {
c gltf.ComponentType
t gltf.AccessorType
count uint32
buffer interface{}
buffer any
}
tests := []struct {
name string
args args
want interface{}
want any
}{
{"nil buffer", args{gltf.ComponentUbyte, gltf.AccessorVec2, 2, nil}, make([][2]uint8, 2)},
{"empty buffer", args{gltf.ComponentUbyte, gltf.AccessorVec2, 2, make([][2]uint8, 0)}, make([][2]uint8, 2)},
Expand Down
2 changes: 1 addition & 1 deletion encode_test.go
Expand Up @@ -676,7 +676,7 @@ func (f *fakeExt) UnmarshalJSON(data []byte) error {
}

func TestExtensions_UnmarshalJSON(t *testing.T) {
RegisterExtension("fake_ext", func(data []byte) (interface{}, error) {
RegisterExtension("fake_ext", func(data []byte) (any, error) {
e := new(fakeExt)
err := json.Unmarshal(data, e)
return e, err
Expand Down
2 changes: 1 addition & 1 deletion ext/lightspuntual/lightspuntual.go
Expand Up @@ -25,7 +25,7 @@ type envelop struct {
}

// Unmarshal decodes the json data into the correct type.
func Unmarshal(data []byte) (interface{}, error) {
func Unmarshal(data []byte) (any, error) {
var env envelop
if err := json.Unmarshal([]byte(data), &env); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion ext/lightspuntual/lightspuntual_test.go
Expand Up @@ -114,7 +114,7 @@ func TestUnmarshal(t *testing.T) {
tests := []struct {
name string
args args
want interface{}
want any
wantErr bool
}{
{"error", args{[]byte(`{"light: 1}`)}, nil, true},
Expand Down
2 changes: 1 addition & 1 deletion ext/specular/specular.go
Expand Up @@ -13,7 +13,7 @@ const (
)

// Unmarshal decodes the json data into the correct type.
func Unmarshal(data []byte) (interface{}, error) {
func Unmarshal(data []byte) (any, error) {
pbr := new(PBRSpecularGlossiness)
err := json.Unmarshal(data, pbr)
return pbr, err
Expand Down
2 changes: 1 addition & 1 deletion ext/specular/specular_test.go
Expand Up @@ -68,7 +68,7 @@ func TestUnmarshal(t *testing.T) {
tests := []struct {
name string
args args
want interface{}
want any
wantErr bool
}{
{"base", args{[]byte("{}")}, &PBRSpecularGlossiness{DiffuseFactor: &[4]float64{1, 1, 1, 1}, SpecularFactor: &[3]float64{1, 1, 1}, GlossinessFactor: gltf.Float(1)}, false},
Expand Down
2 changes: 1 addition & 1 deletion ext/texturetransform/transform.go
Expand Up @@ -20,7 +20,7 @@ const (
)

// Unmarshal decodes the json data into the correct type.
func Unmarshal(data []byte) (interface{}, error) {
func Unmarshal(data []byte) (any, error) {
t := new(TextureTranform)
err := json.Unmarshal(data, t)
return t, err
Expand Down
2 changes: 1 addition & 1 deletion ext/texturetransform/transform_test.go
Expand Up @@ -87,7 +87,7 @@ func TestUnmarshal(t *testing.T) {
tests := []struct {
name string
args args
want interface{}
want any
wantErr bool
}{
{"base", args{[]byte("{}")}, &TextureTranform{Scale: DefaultScale}, false},
Expand Down
4 changes: 2 additions & 2 deletions ext/unlit/unlit.go
Expand Up @@ -12,7 +12,7 @@ const (
)

// Unmarshal decodes the json data into the correct type.
func Unmarshal(data []byte) (interface{}, error) {
func Unmarshal(data []byte) (any, error) {
u := new(Unlit)
err := json.Unmarshal(data, u)
return u, err
Expand All @@ -25,7 +25,7 @@ func init() {
// Unlit defines an unlit shading model.
// When present, the extension indicates that a material should be unlit.
// Additional properties on the extension object are allowed, but may lead to undefined behaviour in conforming viewers.
type Unlit map[string]interface{}
type Unlit map[string]any

// UnmarshalJSON unmarshal the pbr with the correct default values.
func (u *Unlit) UnmarshalJSON(data []byte) error {
Expand Down
2 changes: 1 addition & 1 deletion ext/unlit/unlit_test.go
Expand Up @@ -43,7 +43,7 @@ func TestUnmarshal(t *testing.T) {
tests := []struct {
name string
args args
want interface{}
want any
wantErr bool
}{
{"base", args{[]byte("{}")}, &Unlit{}, false},
Expand Down

0 comments on commit a1c4b99

Please sign in to comment.