From dd252fafae013dada177c4b2566a61d2cfc56d15 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 2 Sep 2023 00:00:12 +0000 Subject: [PATCH] reflection: add initial New() validating type and triggering a scan Signed-off-by: Alejandro Mery --- reflection/reflection.go | 35 +++++++++++++++++++++++++++++++++++ reflection/scan.go | 5 +++++ 2 files changed, 40 insertions(+) create mode 100644 reflection/scan.go diff --git a/reflection/reflection.go b/reflection/reflection.go index 725c207..990b3b3 100644 --- a/reflection/reflection.go +++ b/reflection/reflection.go @@ -1,2 +1,37 @@ // 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 +} + +// 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, + } + + if err := r.scan(); err != nil { + return nil, err + } + + return r, nil +} diff --git a/reflection/scan.go b/reflection/scan.go new file mode 100644 index 0000000..60d84b6 --- /dev/null +++ b/reflection/scan.go @@ -0,0 +1,5 @@ +package reflection + +func (*Reflection) scan() error { + return nil +}