Skip to content

Commit

Permalink
add type res
Browse files Browse the repository at this point in the history
  • Loading branch information
Chise1 committed Sep 2, 2023
1 parent e73e7a1 commit a74b66c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
7 changes: 7 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type Writer interface {
LinkGet(name string) (any, bool)
linkSet(names []string, value any) error // slice map 删除则设置为value nil
linkGet(names []string) (any, bool)
Type() reflect.Type
linkTyp(names []string) (reflect.Type, bool)
LinkTyp(name string) (reflect.Type, bool)
}

// todo add support slice map
Expand Down Expand Up @@ -56,6 +59,7 @@ func subWriter(value any) (writer Writer, err error) {
elem := field.Type.Elem()
if elem.Kind() == reflect.Map {
impl = &mapImpl{
field: field,
mapWriters: make(map[any]Writer),
value: valueOf.Field(i),
}
Expand All @@ -66,11 +70,13 @@ func subWriter(value any) (writer Writer, err error) {
} else if field.Type.Kind() == reflect.Map { // todo暂时不要支持指针?
valueOf.Field(i).Set(reflect.MakeMap(valueOf.Field(i).Type()))
impl = &mapImpl{
field: field,
mapWriters: make(map[any]Writer),
value: valueOf.Field(i),
}
} else if field.Type.Kind() == reflect.Slice {
slice := &sliceImpl{
field: field,
value: valueOf.Field(i),
mapWriters: make(map[any]Writer),
}
Expand Down Expand Up @@ -102,6 +108,7 @@ func subWriter(value any) (writer Writer, err error) {
return &structImpl{
fields: fields,
value: valueOf,
field: valueOf.Type(),
}, nil
}
func NewWriter(value any) (writer Writer, err error) {
Expand Down
9 changes: 9 additions & 0 deletions writer_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ func (s *scalarImpl) LinkSet(_ string, _ any) error {
func (s *scalarImpl) LinkGet(_ string) (any, bool) {
return nil, false
}
func (s *scalarImpl) Type() reflect.Type {
return s.field.Type
}
func (s *scalarImpl) linkTyp(names []string) (reflect.Type, bool) {
return s.Type(), true
}
func (s *scalarImpl) LinkTyp(name string) (reflect.Type, bool) {
return s.Type(), true
}
26 changes: 25 additions & 1 deletion writer_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type mapImpl struct {
mapWriters map[any]Writer
value reflect.Value
field reflect.StructField
}

func (s *mapImpl) Set(value any) (err error) {
Expand Down Expand Up @@ -36,7 +37,12 @@ func (s *mapImpl) linkSet(names []string, value any) error {
if value == nil { //delete
reflect.Indirect(s.value).SetMapIndex(reflect.ValueOf(key), reflect.Value{})
} else {
reflect.Indirect(s.value).SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
switch res := value.(type) {
case reflect.Value:
reflect.Indirect(s.value).SetMapIndex(reflect.ValueOf(key), res)
default:
reflect.Indirect(s.value).SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
}
}
return nil
}
Expand Down Expand Up @@ -109,3 +115,21 @@ func (s *mapImpl) linkGet(names []string) (any, bool) {
}
return writer.linkGet(names[1:])
}
func (s *mapImpl) Type() reflect.Type {
return s.field.Type
}
func (s *mapImpl) linkTyp(names []string) (reflect.Type, bool) {
if len(names) == 0 {
return s.Type(), true
}
var x reflect.Value
x = reflect.New(s.value.Type().Elem())
writer, err := subWriter(x)
if err != nil {
return nil, false // todo 优化报错
}
return writer.linkTyp(names[1:])
}
func (s *mapImpl) LinkTyp(linkName string) (reflect.Type, bool) {
return s.linkTyp(strings.Split(linkName, SqliteSeq))
}
20 changes: 20 additions & 0 deletions writer_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type sliceImpl struct {
value reflect.Value
field reflect.StructField
sliceToMap string //以map形式存储切片
mapWriters map[any]Writer
}
Expand Down Expand Up @@ -128,3 +129,22 @@ func (s *sliceImpl) computeIndex(atoiStr string) (int, error) {
}
return atoi, nil
}

func (s *sliceImpl) Type() reflect.Type {
return s.field.Type
}
func (s *sliceImpl) linkTyp(names []string) (reflect.Type, bool) {
if len(names) == 0 {
return s.Type(), true
}
ints := reflect.Zero(s.value.Type().Elem())
writer, err := subWriter(ints)
if err == nil {
return writer.linkTyp(names[1:])
}
return nil, false
}
func (s *sliceImpl) LinkTyp(linkName string) (reflect.Type, bool) {
return s.linkTyp(strings.Split(linkName, SqliteSeq))

}
20 changes: 19 additions & 1 deletion writer_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type structImpl struct {
fields map[string]Writer
value reflect.Value
writer Writer
field reflect.Type
}

func (s *structImpl) Set(value any) (err error) {
Expand Down Expand Up @@ -59,3 +59,21 @@ func (s *structImpl) linkGet(names []string) (any, bool) {
}
return field.linkGet(names[1:])
}

func (s *structImpl) Type() reflect.Type {
return s.field
}
func (s *structImpl) linkTyp(names []string) (reflect.Type, bool) {
if len(names) == 0 {
return s.Type(), true
}
name := names[0]
field, ok := s.fields[name]
if !ok {
return nil, false
}
return field.linkTyp(names[1:])
}
func (s *structImpl) LinkTyp(name string) (reflect.Type, bool) {
return s.linkTyp(strings.Split(name, SqliteSeq))
}
17 changes: 17 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,21 @@ func TestMapImpl(t *testing.T) {
val, found = writer.LinkGet("M2")
assert.Equal(t, true, found)
assert.Equal(t, "map[1:{1 lisi}]", fmt.Sprint(val))
data := `{"Index":33,"Name":"ff33"}`
typ, ok := writer.LinkTyp("M2.1")
assert.Equal(t, true, ok)
instance := NewObj(typ)
assert.Equal(t, true, found)
err = json.Unmarshal([]byte(data), &instance)
assert.Equal(t, nil, err)

assert.Equal(t, true, found)
assert.Equal(t, "&{33 ff33}", fmt.Sprint(instance))
err = writer.LinkSet("M2.33", reflect.Indirect(reflect.ValueOf(instance)))
assert.Equal(t, nil, err)
assert.Equal(t, "{0 map[1:{1 lisi} 33:{33 ff33}] map[zhangsan:ei]}", fmt.Sprint(s))

}
func NewObj(t reflect.Type) any {
return reflect.New(t).Interface()
}

0 comments on commit a74b66c

Please sign in to comment.