Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a38f0c74e8 | |||
| 7e0b7cfb47 | |||
| fd4f6439b8 | |||
| f74745471d |
@@ -2,22 +2,20 @@ module asciigoat.org/core
|
|||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require github.com/mgechev/revive v1.3.3
|
||||||
github.com/fatih/structtag v1.2.0
|
|
||||||
github.com/mgechev/revive v1.3.3
|
|
||||||
github.com/pkg/errors v0.9.1
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||||
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab // indirect
|
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab // indirect
|
||||||
github.com/fatih/color v1.15.0 // indirect
|
github.com/fatih/color v1.15.0 // indirect
|
||||||
|
github.com/fatih/structtag v1.2.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 // indirect
|
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 // indirect
|
||||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
golang.org/x/sys v0.11.0 // indirect
|
golang.org/x/sys v0.11.0 // indirect
|
||||||
golang.org/x/tools v0.12.0 // indirect
|
golang.org/x/tools v0.12.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
package reflection
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
)
|
|
||||||
|
|
||||||
func structFieldName(sf reflect.StructField) string {
|
|
||||||
if sf.Anonymous {
|
|
||||||
idx := sf.Index[len(sf.Index)-1]
|
|
||||||
|
|
||||||
return fmt.Sprintf("%s-%v", "anonymous", idx)
|
|
||||||
}
|
|
||||||
|
|
||||||
return sf.Name
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
// Package reflection helps Marshalling/Unmarshalling data to structs
|
|
||||||
package reflection
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Reflection provides Marshalling/Unmarshalling oriented view
|
|
||||||
// of a value
|
|
||||||
type Reflection struct {
|
|
||||||
v reflect.Value
|
|
||||||
|
|
||||||
types map[reflect.Type]TypeInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
// Value returns the object it reflects
|
|
||||||
func (r *Reflection) Value() any {
|
|
||||||
return r.v.Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a Reflection of the given pointer
|
|
||||||
func New(v any) (*Reflection, error) {
|
|
||||||
rv := reflect.ValueOf(v)
|
|
||||||
|
|
||||||
if rv.Kind() != reflect.Pointer || rv.IsNil() {
|
|
||||||
err := &InvalidUnmarshalError{Type: rv.Type()}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
r := &Reflection{
|
|
||||||
v: rv,
|
|
||||||
types: make(map[reflect.Type]TypeInfo),
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := r.scan(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return r, nil
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package reflection
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (r *Reflection) scanType(rt reflect.Type) error {
|
|
||||||
switch rt.Kind() {
|
|
||||||
case reflect.Pointer, reflect.Slice:
|
|
||||||
rt = rt.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := r.types[rt]; ok {
|
|
||||||
// cached
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ti, err := NewTypeInfo(rt)
|
|
||||||
if err != nil {
|
|
||||||
return &UnmarshalTypeError{Type: rt, Err: err}
|
|
||||||
}
|
|
||||||
|
|
||||||
r.types[rt] = ti
|
|
||||||
|
|
||||||
for i := range ti.fields {
|
|
||||||
err := r.scanType(ti.fields[i].sf.Type)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Reflection) scan() error {
|
|
||||||
return r.scanType(r.v.Type())
|
|
||||||
}
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
package reflection
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/fatih/structtag"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TypeInfo ...
|
|
||||||
type TypeInfo struct {
|
|
||||||
rt reflect.Type
|
|
||||||
|
|
||||||
fields []TypeInfoField
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type ...
|
|
||||||
func (ti *TypeInfo) Type() reflect.Type {
|
|
||||||
return ti.rt
|
|
||||||
}
|
|
||||||
|
|
||||||
// Kind ...
|
|
||||||
func (ti *TypeInfo) Kind() reflect.Kind {
|
|
||||||
return ti.rt.Kind()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TypeInfoField ...
|
|
||||||
type TypeInfoField struct {
|
|
||||||
sf reflect.StructField
|
|
||||||
tags *structtag.Tags
|
|
||||||
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type ...
|
|
||||||
func (tif *TypeInfoField) Type() reflect.Type {
|
|
||||||
return tif.sf.Type
|
|
||||||
}
|
|
||||||
|
|
||||||
// Kind ...
|
|
||||||
func (tif *TypeInfoField) Kind() reflect.Kind {
|
|
||||||
return tif.sf.Type.Kind()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tag ...
|
|
||||||
func (tif *TypeInfoField) Tag(key string) (*structtag.Tag, bool) {
|
|
||||||
if tif.tags != nil {
|
|
||||||
tag, _ := tif.tags.Get(key)
|
|
||||||
if tag != nil {
|
|
||||||
return tag, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewTypeInfo ...
|
|
||||||
func NewTypeInfo(rt reflect.Type) (TypeInfo, error) {
|
|
||||||
log.Printf("%s.%s: %s (%s)", "reflection", "NewTypeInfo", rt, rt.Kind())
|
|
||||||
|
|
||||||
ti := TypeInfo{
|
|
||||||
rt: rt,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := ti.init()
|
|
||||||
return ti, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ti *TypeInfo) init() error {
|
|
||||||
switch ti.Kind() {
|
|
||||||
case reflect.Struct:
|
|
||||||
return ti.initStruct()
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ti *TypeInfo) initStruct() error {
|
|
||||||
// load fields
|
|
||||||
n := ti.rt.NumField()
|
|
||||||
fields := make([]TypeInfoField, 0, n)
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
sf := ti.rt.Field(i)
|
|
||||||
tags, err := structtag.Parse(string(sf.Tag))
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
// tag parse error
|
|
||||||
err = &UnmarshalTypeError{
|
|
||||||
Type: ti.rt,
|
|
||||||
Err: errors.Wrap(err, structFieldName(sf)),
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if sf.IsExported() {
|
|
||||||
log.Printf("%s.%s: [%v]: %q %s (%s)",
|
|
||||||
"reflection", "NewTypeInfo",
|
|
||||||
i, structFieldName(sf), sf.Type, sf.Type.Kind())
|
|
||||||
|
|
||||||
fields = append(fields, TypeInfoField{
|
|
||||||
sf: sf,
|
|
||||||
tags: tags,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ti.fields = fields
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
// Package reflective assists reflection-driven unmarshalling
|
||||||
|
package reflective
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package reflection
|
package reflective
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package reflective
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
// Reflection provides Marshalling/Unmarshalling oriented view
|
||||||
|
// of a value
|
||||||
|
type Reflection struct {
|
||||||
|
v reflect.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a Reflection of the given pointer
|
||||||
|
func New(v any) (*Reflection, error) {
|
||||||
|
rv := reflect.ValueOf(v)
|
||||||
|
|
||||||
|
if rv.Kind() != reflect.Pointer || rv.IsNil() {
|
||||||
|
err := &InvalidUnmarshalError{
|
||||||
|
Type: rv.Type(),
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
r := &Reflection{
|
||||||
|
v: rv,
|
||||||
|
}
|
||||||
|
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user