From a38f0c74e87631c05c11d4ea22cb09cf4dd8ef83 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 2 Sep 2023 16:18:56 +0000 Subject: [PATCH] reflective: Add Reflection{} placeholder and the New() factory Signed-off-by: Alejandro Mery --- reflective/reflection.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 reflective/reflection.go diff --git a/reflective/reflection.go b/reflective/reflection.go new file mode 100644 index 0000000..2f70f6c --- /dev/null +++ b/reflective/reflection.go @@ -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 +}