Skip to content

Commit

Permalink
chore: remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
sranka committed Nov 29, 2023
1 parent 925548b commit f168bda
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
10 changes: 7 additions & 3 deletions api/data_to_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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 != "" {
Expand Down
43 changes: 0 additions & 43 deletions api/reflection.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit f168bda

Please sign in to comment.