Skip to content

Commit

Permalink
added more functionality to asserted typed values
Browse files Browse the repository at this point in the history
  • Loading branch information
martinconic committed Jan 18, 2024
1 parent 25dacf5 commit 0d6482a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion interfacestruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (dt *DataGeneric[T]) ConvertToStruct(data []interface{}) (T, error) {
if i >= len(data) {
return dt.Data, fmt.Errorf("not enough data to populate all fields")
}

// Convert the interface value to the type of the struct field
if reflect.TypeOf(data[i]) != fieldType.Type {
value, err := getAssertedTypedValue(data[i], fieldType)
Expand All @@ -65,6 +64,15 @@ func getAssertedTypedValue(data interface{}, fieldType reflect.StructField) (ref
if fieldType.Type.Kind() == reflect.Int {
d, _ := strconv.Atoi(data.(string))
return reflect.ValueOf(d), nil
} else if fieldType.Type.Kind() == reflect.Uint64 {
d, _ := strconv.ParseUint(data.(string), 10, 64)
return reflect.ValueOf(d), nil
} else if fieldType.Type.Kind() == reflect.Float32 {
d, _ := strconv.ParseFloat(data.(string), 32)
return reflect.ValueOf(d), nil
} else if fieldType.Type.Kind() == reflect.Float64 {
d, _ := strconv.ParseFloat(data.(string), 64)
return reflect.ValueOf(d), nil
} else if fieldType.Type == reflect.TypeOf(time.Time{}) {
layout := "2006-01-02T15:04:05.999999999Z07:00"
d, _ := time.Parse(layout, data.(string))
Expand Down

0 comments on commit 0d6482a

Please # to comment.