From f168bda5a938edc54114da99035a4d3b0d42779b Mon Sep 17 00:00:00 2001 From: Pavel Zavora Date: Wed, 29 Nov 2023 12:16:51 +0100 Subject: [PATCH] chore: remove unused code --- api/data_to_point.go | 10 +++++++--- api/reflection.go | 43 ------------------------------------------- 2 files changed, 7 insertions(+), 46 deletions(-) diff --git a/api/data_to_point.go b/api/data_to_point.go index 8ecc71c..4977958 100644 --- a/api/data_to_point.go +++ b/api/data_to_point.go @@ -26,15 +26,15 @@ import ( // Description string `lp:"-"` // } func DataToPoint(x interface{}) (*write.Point, error) { - if err := checkContainerType(x, false, "point"); err != nil { - return nil, err - } t := reflect.TypeOf(x) v := reflect.ValueOf(x) if t.Kind() == reflect.Ptr { t = t.Elem() v = v.Elem() } + if t.Kind() != reflect.Struct { + return nil, fmt.Errorf("cannot use %v as point", t) + } fields := reflect.VisibleFields(t) var measurement = "" @@ -56,6 +56,10 @@ func DataToPoint(x interface{}) (*write.Point, error) { if len(parts) == 2 { name = parts[1] } + t := getFieldType(v.FieldByIndex(f.Index)) + if !validFieldType(t) { + return nil, fmt.Errorf("cannot use field '%s' of type '%v' as to create a point", f.Name, t) + } switch typ { case "measurement": if measurement != "" { diff --git a/api/reflection.go b/api/reflection.go index e012b5f..8becc40 100644 --- a/api/reflection.go +++ b/api/reflection.go @@ -1,53 +1,10 @@ package api import ( - "fmt" "reflect" "time" ) -// checkContainerType validates the value is struct with simple type fields -// or a map with key as string and value as a simple type -func checkContainerType(p interface{}, alsoMap bool, usage string) error { - if p == nil { - return nil - } - t := reflect.TypeOf(p) - v := reflect.ValueOf(p) - if t.Kind() == reflect.Ptr { - t = t.Elem() - v = v.Elem() - } - if t.Kind() != reflect.Struct && (!alsoMap || t.Kind() != reflect.Map) { - return fmt.Errorf("cannot use %v as %s", t, usage) - } - switch t.Kind() { - case reflect.Struct: - fields := reflect.VisibleFields(t) - for _, f := range fields { - fv := v.FieldByIndex(f.Index) - t := getFieldType(fv) - if !validFieldType(t) { - return fmt.Errorf("cannot use field '%s' of type '%v' as a %s", f.Name, t, usage) - } - - } - case reflect.Map: - key := t.Key() - if key.Kind() != reflect.String { - return fmt.Errorf("cannot use map key of type '%v' for %s name", key, usage) - } - for _, k := range v.MapKeys() { - f := v.MapIndex(k) - t := getFieldType(f) - if !validFieldType(t) { - return fmt.Errorf("cannot use map value type '%v' as a %s", t, usage) - } - } - } - return nil -} - // getFieldType extracts type of value func getFieldType(v reflect.Value) reflect.Type { t := v.Type()