Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a38f0c74e8 | |||
| 7e0b7cfb47 | |||
| fd4f6439b8 | |||
| f74745471d |
@@ -0,0 +1,2 @@
|
||||
// Package reflective assists reflection-driven unmarshalling
|
||||
package reflective
|
||||
@@ -0,0 +1,69 @@
|
||||
package reflective
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// An InvalidUnmarshalError describes an invalid argument passed to New.
|
||||
// (It must be a non-nil pointer.)
|
||||
type InvalidUnmarshalError struct {
|
||||
Method string
|
||||
Prefix string
|
||||
|
||||
reflect.Type
|
||||
}
|
||||
|
||||
func (e *InvalidUnmarshalError) Error() string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if e.Prefix != "" {
|
||||
_, _ = fmt.Fprintf(&buf, "%s: ", e.Prefix)
|
||||
}
|
||||
|
||||
method := e.Method
|
||||
if method == "" {
|
||||
method = "New"
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintf(&buf, "%s(", method)
|
||||
|
||||
switch {
|
||||
case e.Type == nil:
|
||||
_, _ = buf.WriteString("nil")
|
||||
case e.Type.Kind() != reflect.Pointer:
|
||||
_, _ = fmt.Fprintf(&buf, "%s %s", "non-pointer", e.Type.String())
|
||||
default:
|
||||
_, _ = fmt.Fprintf(&buf, "%s %s", "nil", e.Type.String())
|
||||
}
|
||||
|
||||
_, _ = buf.WriteString(")")
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// An UnmarshalTypeError tells something went wrong while processing
|
||||
// a type.
|
||||
type UnmarshalTypeError struct {
|
||||
Prefix string
|
||||
Err error
|
||||
|
||||
reflect.Type
|
||||
}
|
||||
|
||||
func (e UnmarshalTypeError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
func (e UnmarshalTypeError) Error() string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if e.Prefix != "" {
|
||||
_, _ = fmt.Fprintf(&buf, "%s: ", e.Prefix)
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintf(&buf, "%s: %s", e.Type.String(), e.Err)
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
@@ -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