Skip to content
Draft
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
22 changes: 11 additions & 11 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func BenchmarkGetterHas_String(b *testing.B) {
}

func BenchmarkGetterGet_String(b *testing.B) {
var it interface{}
var it any

g, err := newTestGetter() // See: getter_test.go
if err != nil {
Expand Down Expand Up @@ -170,7 +170,7 @@ func BenchmarkGetterUnsafePointer(b *testing.B) {
}

func BenchmarkGetterSlice_StructPtrSlice(b *testing.B) {
var sl []interface{}
var sl []any

g, err := newTestGetter() // See: getter_test.go
if err != nil {
Expand Down Expand Up @@ -202,7 +202,7 @@ func BenchmarkGetterGetGetter(b *testing.B) {
}

func BenchmarkGetterToMap(b *testing.B) {
var m map[string]interface{}
var m map[string]any

g, err := newTestGetter() // See: getter_test.go
if err != nil {
Expand Down Expand Up @@ -282,14 +282,14 @@ func BenchmarkGetterIsSlice_StructPtrSlice(b *testing.B) {
}

func BenchmarkGetterMapGet(b *testing.B) {
var ia []interface{}
var ia []any

g, err := newTestGetter() // See: getter_test.go
if err != nil {
b.Fatalf("NewGetter() occurs unexpected error: %v", err)
return
}
fn := func(i int, g *Getter) (interface{}, error) {
fn := func(i int, g *Getter) (any, error) {
str, _ := g.String("String")
str2, _ := g.String("String")
return fmt.Sprintf("%s:%s", str, str2), nil
Expand Down Expand Up @@ -339,7 +339,7 @@ func BenchmarkNewFinder_Ptr(b *testing.B) {
}

func BenchmarkToMap_1FindOnly(b *testing.B) {
var m map[string]interface{}
var m map[string]any

f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go
if err != nil {
Expand All @@ -359,7 +359,7 @@ func BenchmarkToMap_1FindOnly(b *testing.B) {
}

func BenchmarkToMap_2FindOnly(b *testing.B) {
var m map[string]interface{}
var m map[string]any

f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go
if err != nil {
Expand All @@ -379,7 +379,7 @@ func BenchmarkToMap_2FindOnly(b *testing.B) {
}

func BenchmarkToMap_1Struct_1Find(b *testing.B) {
var m map[string]interface{}
var m map[string]any

f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go
if err != nil {
Expand All @@ -399,7 +399,7 @@ func BenchmarkToMap_1Struct_1Find(b *testing.B) {
}

func BenchmarkToMap_1Struct_1Find_2Pair(b *testing.B) {
var m map[string]interface{}
var m map[string]any

f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go
if err != nil {
Expand All @@ -419,7 +419,7 @@ func BenchmarkToMap_1Struct_1Find_2Pair(b *testing.B) {
}

func BenchmarkToMap_2Struct_1Find(b *testing.B) {
var m map[string]interface{}
var m map[string]any

f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go
if err != nil {
Expand All @@ -439,7 +439,7 @@ func BenchmarkToMap_2Struct_1Find(b *testing.B) {
}

func BenchmarkToMap_2Struct_2Find(b *testing.B) {
var m map[string]interface{}
var m map[string]any

f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go
if err != nil {
Expand Down
31 changes: 30 additions & 1 deletion cmd/strucmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,37 @@ package main

import (
"fmt"
"io"
"os"

"github.com/goldeneggg/structil/dynamicstruct/decoder"
)

func main() {
fmt.Println("NOT IMPLEMENTED YET")
// fmt.Println("NOT IMPLEMENTED YET")

f, err := os.Open("demo.json")
if err != nil {
panic(err)
}

b, err := io.ReadAll(f)
if err != nil {
panic(err)
}

dec, err := decoder.FromJSON(b)
if err != nil {
panic(err)
}

nest := true
useTag := true
ds, err := dec.DynamicStruct(nest, useTag)
if err != nil {
panic(err)
}

// Print struct definition from DynamicStruct
fmt.Println(ds.Definition())
}
2 changes: 1 addition & 1 deletion dynamicstruct/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func BenchmarkAddMap(b *testing.B) {
func BenchmarkAddFunc(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = NewBuilder().AddFunc("FuncField", []interface{}{SampleInt, SampleInt}, []interface{}{SampleBool, ErrSample})
_ = NewBuilder().AddFunc("FuncField", []any{SampleInt, SampleInt}, []any{SampleBool, ErrSample})
}
}

Expand Down
40 changes: 20 additions & 20 deletions dynamicstruct/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,19 @@ func (b *Builder) AddBoolWithTag(name string, tag string) *Builder {
// AddMap returns a Builder that was added a map field named by name parameter.
// Type of map key is type of ki.
// Type of map value is type of vi.
func (b *Builder) AddMap(name string, ki interface{}, vi interface{}) *Builder {
func (b *Builder) AddMap(name string, ki any, vi any) *Builder {
b.AddMapWithTag(name, ki, vi, "")
return b
}

// AddMapWithTag returns a Builder that was added a map field with tag named by name parameter.
// Type of map key is type of ki.
// Type of map value is type of vi.
func (b *Builder) AddMapWithTag(name string, ki interface{}, vi interface{}, tag string) *Builder {
func (b *Builder) AddMapWithTag(name string, ki any, vi any, tag string) *Builder {
f := func() reflect.Type {
var vt reflect.Type
if vi == nil {
vt = reflect.TypeOf((*interface{})(nil)).Elem()
vt = reflect.TypeOf((*any)(nil)).Elem()
} else {
vt = reflect.TypeOf(vi)
}
Expand All @@ -225,15 +225,15 @@ func (b *Builder) AddMapWithTag(name string, ki interface{}, vi interface{}, tag
// AddFunc returns a Builder that was added a func field named by name parameter.
// Types of func args are types of in.
// Types of func returns are types of out.
func (b *Builder) AddFunc(name string, in []interface{}, out []interface{}) *Builder {
func (b *Builder) AddFunc(name string, in []any, out []any) *Builder {
b.AddFuncWithTag(name, in, out, "")
return b
}

// AddFuncWithTag returns a Builder that was added a func field with tag named by name parameter.
// Types of func args are types of in.
// Types of func returns are types of out.
func (b *Builder) AddFuncWithTag(name string, in []interface{}, out []interface{}, tag string) *Builder {
func (b *Builder) AddFuncWithTag(name string, in []any, out []any, tag string) *Builder {
f := func() reflect.Type {
it := make([]reflect.Type, len(in))
for i := 0; i < len(in); i++ {
Expand All @@ -252,14 +252,14 @@ func (b *Builder) AddFuncWithTag(name string, in []interface{}, out []interface{

// AddChanBoth returns a Builder that was added a BothDir chan field named by name parameter.
// Type of chan is type of i.
func (b *Builder) AddChanBoth(name string, i interface{}) *Builder {
func (b *Builder) AddChanBoth(name string, i any) *Builder {
b.AddChanBothWithTag(name, i, "")
return b
}

// AddChanBothWithTag returns a Builder that was added a BothDir chan field with tag named by name parameter.
// Type of chan is type of i.
func (b *Builder) AddChanBothWithTag(name string, i interface{}, tag string) *Builder {
func (b *Builder) AddChanBothWithTag(name string, i any, tag string) *Builder {
f := func() reflect.Type {
return reflect.ChanOf(reflect.BothDir, reflect.TypeOf(i))
}
Expand All @@ -270,14 +270,14 @@ func (b *Builder) AddChanBothWithTag(name string, i interface{}, tag string) *Bu

// AddChanRecv returns a Builder that was added a RecvDir chan field named by name parameter.
// Type of chan is type of i.
func (b *Builder) AddChanRecv(name string, i interface{}) *Builder {
func (b *Builder) AddChanRecv(name string, i any) *Builder {
b.AddChanRecvWithTag(name, i, "")
return b
}

// AddChanRecvWithTag returns a Builder that was added a RecvDir chan field with tag named by name parameter.
// Type of chan is type of i.
func (b *Builder) AddChanRecvWithTag(name string, i interface{}, tag string) *Builder {
func (b *Builder) AddChanRecvWithTag(name string, i any, tag string) *Builder {
f := func() reflect.Type {
return reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(i))
}
Expand All @@ -288,14 +288,14 @@ func (b *Builder) AddChanRecvWithTag(name string, i interface{}, tag string) *Bu

// AddChanSend returns a Builder that was added a SendDir chan field named by name parameter.
// Type of chan is type of i.
func (b *Builder) AddChanSend(name string, i interface{}) *Builder {
func (b *Builder) AddChanSend(name string, i any) *Builder {
b.AddChanSendWithTag(name, i, "")
return b
}

// AddChanSendWithTag returns a Builder that was added a SendDir chan field with tag named by name parameter.
// Type of chan is type of i.
func (b *Builder) AddChanSendWithTag(name string, i interface{}, tag string) *Builder {
func (b *Builder) AddChanSendWithTag(name string, i any, tag string) *Builder {
f := func() reflect.Type {
return reflect.ChanOf(reflect.SendDir, reflect.TypeOf(i))
}
Expand All @@ -306,14 +306,14 @@ func (b *Builder) AddChanSendWithTag(name string, i interface{}, tag string) *Bu

// AddStruct returns a Builder that was added a struct field named by name parameter.
// Type of struct is type of i.
func (b *Builder) AddStruct(name string, i interface{}, isPtr bool) *Builder {
func (b *Builder) AddStruct(name string, i any, isPtr bool) *Builder {
b.AddStructWithTag(name, i, isPtr, "")
return b
}

// AddStructWithTag returns a Builder that was added a struct field with tag named by name parameter.
// Type of struct is type of i.
func (b *Builder) AddStructWithTag(name string, i interface{}, isPtr bool, tag string) *Builder {
func (b *Builder) AddStructWithTag(name string, i any, isPtr bool, tag string) *Builder {
f := func() reflect.Type {
iType := reflect.TypeOf(i)
if iType.Kind() == reflect.Ptr {
Expand All @@ -332,26 +332,26 @@ func (b *Builder) AddStructWithTag(name string, i interface{}, isPtr bool, tag s

// AddStructPtr returns a Builder that was added a struct pointer field named by name parameter.
// Type of struct is type of i.
func (b *Builder) AddStructPtr(name string, i interface{}) *Builder {
func (b *Builder) AddStructPtr(name string, i any) *Builder {
return b.AddStruct(name, i, true)
}

// AddStructPtrWithTag returns a Builder that was added a struct pointer field with tag named by name parameter.
// Type of struct is type of i.
func (b *Builder) AddStructPtrWithTag(name string, i interface{}, tag string) *Builder {
func (b *Builder) AddStructPtrWithTag(name string, i any, tag string) *Builder {
return b.AddStructWithTag(name, i, true, tag)
}

// AddSlice returns a Builder that was added a slice field named by name parameter.
// Type of slice is type of i.
func (b *Builder) AddSlice(name string, i interface{}) *Builder {
func (b *Builder) AddSlice(name string, i any) *Builder {
b.AddSliceWithTag(name, i, "")
return b
}

// AddSliceWithTag returns a Builder that was added a slice field with tag named by name parameter.
// Type of slice is type of i.
func (b *Builder) AddSliceWithTag(name string, i interface{}, tag string) *Builder {
func (b *Builder) AddSliceWithTag(name string, i any, tag string) *Builder {
f := func() reflect.Type {
return reflect.SliceOf(reflect.TypeOf(i))
}
Expand All @@ -360,16 +360,16 @@ func (b *Builder) AddSliceWithTag(name string, i interface{}, tag string) *Build
return b
}

// AddInterface returns a Builder that was added a interface{} field named by name parameter.
// AddInterface returns a Builder that was added a any field named by name parameter.
func (b *Builder) AddInterface(name string, isPtr bool) *Builder {
b.AddInterfaceWithTag(name, isPtr, "")
return b
}

// AddInterfaceWithTag returns a Builder that was added a interface{} field with tag named by name parameter.
// AddInterfaceWithTag returns a Builder that was added a any field with tag named by name parameter.
func (b *Builder) AddInterfaceWithTag(name string, isPtr bool, tag string) *Builder {
f := func() reflect.Type {
return reflect.TypeOf((*interface{})(nil)).Elem()
return reflect.TypeOf((*any)(nil)).Elem()
}
b.addFieldFunc(name, isPtr, tag, f)

Expand Down
18 changes: 9 additions & 9 deletions dynamicstruct/decoder/data_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ func (dt dataType) string() string {
return ""
}

func (dt dataType) unmarshal(data []byte) (interface{}, error) {
var intf interface{}
func (dt dataType) unmarshal(data []byte) (any, error) {
var intf any
err := dt.unmarshalWithIPtr(data, &intf)
return intf, err
}

func (dt dataType) unmarshalWithIPtr(data []byte, iptr interface{}) error {
func (dt dataType) unmarshalWithIPtr(data []byte, iptr any) error {
var err error

switch dt {
case typeJSON:
// Note: iptr should be "map[string]interface{}"
// Note: iptr should be "map[string]any"
err = json.Unmarshal(data, iptr)
case typeYAML:
// Note: iptr should be "map[interface{}]interface{}" using gopkg.in/yaml.v2 package
// Note: iptr should be "map[any]any" using gopkg.in/yaml.v2 package
err = yaml.Unmarshal(data, iptr)
default:
err = fmt.Errorf("invalid datatype for Unmarshal: %v", dt)
Expand All @@ -68,14 +68,14 @@ func (dt dataType) unmarshalWithIPtr(data []byte, iptr interface{}) error {
}

// TODO: add tests and examples
// func (dt dataType) marshal(v interface{}) (data []byte, err error) {
func (dt dataType) marshal(m map[string]interface{}) (data []byte, err error) {
// func (dt dataType) marshal(v any) (data []byte, err error) {
func (dt dataType) marshal(m map[string]any) (data []byte, err error) {
switch dt {
case typeJSON:
// Note: v is expected to be "map[string]interface{}"
// Note: v is expected to be "map[string]any"
data, err = json.Marshal(m)
case typeYAML:
// Note: v is expected to be converted from "map[interface{}]interface{}" to "map[string]interface{}"
// Note: v is expected to be converted from "map[any]any" to "map[string]any"
data, err = yaml.Marshal(m)
default:
err = fmt.Errorf("invalid datatype for Marshal: %v", dt)
Expand Down
Loading