MCPcopy
hub / github.com/g3n/engine / parseFloats

Method parseFloats

gui/builder.go:1056–1105  ·  view source on GitHub ↗

parseFloats parses a string with a list of floats with the specified size and returns a slice. The specified size is 0 any number of floats is allowed. The individual values can be separated by spaces or commas

(am map[string]interface{}, fname string, min, max int)

Source from the content-addressed store, hash-verified

1054// and returns a slice. The specified size is 0 any number of floats is allowed.
1055// The individual values can be separated by spaces or commas
1056func (b *Builder) parseFloats(am map[string]interface{}, fname string, min, max int) ([]float32, error) {
1057
1058 // Checks if field is empty
1059 v := am[fname]
1060 if v == nil {
1061 return nil, nil
1062 }
1063
1064 // If field has only one value, it is an int or a float64
1065 switch ft := v.(type) {
1066 case int:
1067 return []float32{float32(ft)}, nil
1068 case float64:
1069 return []float32{float32(ft)}, nil
1070 }
1071
1072 // Converts to string
1073 fs, ok := v.(string)
1074 if !ok {
1075 return nil, b.err(am, fname, "Not a string")
1076 }
1077
1078 // Checks if string field is empty
1079 fs = strings.Trim(fs, " ")
1080 if fs == "" {
1081 return nil, nil
1082 }
1083
1084 // Separate individual fields
1085 var parts []string
1086 if strings.Index(fs, ",") < 0 {
1087 parts = strings.Fields(fs)
1088 } else {
1089 parts = strings.Split(fs, ",")
1090 }
1091 if len(parts) < min || len(parts) > max {
1092 return nil, b.err(am, fname, "Invalid number of float32 values")
1093 }
1094
1095 // Parse each field value and appends to slice
1096 var values []float32
1097 for i := 0; i < len(parts); i++ {
1098 val, err := strconv.ParseFloat(strings.Trim(parts[i], " "), 32)
1099 if err != nil {
1100 return nil, b.err(am, fname, err.Error())
1101 }
1102 values = append(values, float32(val))
1103 }
1104 return values, nil
1105}
1106
1107// err creates and returns an error for the current object, field name and with the specified message
1108func (b *Builder) err(am map[string]interface{}, fname, msg string) error {

Callers 3

AttribCheckColorFunction · 0.80
AttribCheckBorderSizesFunction · 0.80
AttribCheckPositionFunction · 0.80

Calls 4

errMethod · 0.95
SplitMethod · 0.80
ErrorMethod · 0.80
IndexMethod · 0.65

Tested by

no test coverage detected