forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/brianvoe/gofakeit/v6/lookup.go

849 lines
12 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
package gofakeit
import (
"encoding/json"
"fmt"
"math/rand"
"reflect"
"strconv"
"strings"
"sync"
)
// FuncLookups is the primary map array with mapping to all available data
2024-04-26 19:30:35 +00:00
var FuncLookups map[string]Info
2024-04-26 19:30:35 +00:00
var lockFuncLookups sync.Mutex
// MapParams is the values to pass into a lookup generate
2024-04-26 19:30:35 +00:00
type MapParams map[string]MapParamsValue
type MapParamsValue []string
// Info structures fields to better break down what each one generates
2024-04-26 19:30:35 +00:00
type Info struct {
Display string `json:"display"`
Category string `json:"category"`
Description string `json:"description"`
Example string `json:"example"`
Output string `json:"output"`
ContentType string `json:"content_type"`
Params []Param `json:"params"`
Any any `json:"any"`
Generate func(r *rand.Rand, m *MapParams, info *Info) (any, error) `json:"-"`
2024-04-26 19:30:35 +00:00
}
// Param is a breakdown of param requirements and type definition
2024-04-26 19:30:35 +00:00
type Param struct {
Field string `json:"field"`
Display string `json:"display"`
Type string `json:"type"`
Optional bool `json:"optional"`
Default string `json:"default"`
Options []string `json:"options"`
Description string `json:"description"`
2024-04-26 19:30:35 +00:00
}
// Field is used for defining what name and function you to generate for file outuputs
2024-04-26 19:30:35 +00:00
type Field struct {
Name string `json:"name"`
Function string `json:"function"`
Params MapParams `json:"params"`
2024-04-26 19:30:35 +00:00
}
func init() { initLookup() }
// init will add all the functions to MapLookups
2024-04-26 19:30:35 +00:00
func initLookup() {
2024-04-26 19:30:35 +00:00
addAddressLookup()
2024-04-26 19:30:35 +00:00
addAnimalLookup()
2024-04-26 19:30:35 +00:00
addAppLookup()
2024-04-26 19:30:35 +00:00
addAuthLookup()
2024-04-26 19:30:35 +00:00
addBeerLookup()
2024-04-26 19:30:35 +00:00
addBookLookup()
2024-04-26 19:30:35 +00:00
addCarLookup()
2024-04-26 19:30:35 +00:00
addCelebrityLookup()
2024-04-26 19:30:35 +00:00
addColorLookup()
2024-04-26 19:30:35 +00:00
addCompanyLookup()
2024-04-26 19:30:35 +00:00
addDatabaseSQLLookup()
2024-04-26 19:30:35 +00:00
addDateTimeLookup()
2024-04-26 19:30:35 +00:00
addEmojiLookup()
2024-04-26 19:30:35 +00:00
addErrorLookup()
2024-04-26 19:30:35 +00:00
addFileCSVLookup()
2024-04-26 19:30:35 +00:00
addFileJSONLookup()
2024-04-26 19:30:35 +00:00
addFileLookup()
2024-04-26 19:30:35 +00:00
addFileXMLLookup()
2024-04-26 19:30:35 +00:00
addFinanceLookup()
2024-04-26 19:30:35 +00:00
addFoodLookup()
2024-04-26 19:30:35 +00:00
addGameLookup()
2024-04-26 19:30:35 +00:00
addGenerateLookup()
2024-04-26 19:30:35 +00:00
addHackerLookup()
2024-04-26 19:30:35 +00:00
addHipsterLookup()
2024-04-26 19:30:35 +00:00
addHtmlLookup()
2024-04-26 19:30:35 +00:00
addImageLookup()
2024-04-26 19:30:35 +00:00
addInternetLookup()
2024-04-26 19:30:35 +00:00
addLanguagesLookup()
2024-04-26 19:30:35 +00:00
addLoremLookup()
2024-04-26 19:30:35 +00:00
addMinecraftLookup()
2024-04-26 19:30:35 +00:00
addMiscLookup()
2024-04-26 19:30:35 +00:00
addMovieLookup()
2024-04-26 19:30:35 +00:00
addNumberLookup()
2024-04-26 19:30:35 +00:00
addPaymentLookup()
2024-04-26 19:30:35 +00:00
addPersonLookup()
2024-04-26 19:30:35 +00:00
addProductLookup()
2024-04-26 19:30:35 +00:00
addSchoolLookup()
2024-04-26 19:30:35 +00:00
addStringLookup()
2024-04-26 19:30:35 +00:00
addTemplateLookup()
2024-04-26 19:30:35 +00:00
addWeightedLookup()
2024-04-26 19:30:35 +00:00
addWordAdjectiveLookup()
2024-04-26 19:30:35 +00:00
addWordAdverbLookup()
2024-04-26 19:30:35 +00:00
addWordConnectiveLookup()
2024-04-26 19:30:35 +00:00
addWordGeneralLookup()
2024-04-26 19:30:35 +00:00
addWordGrammerLookup()
2024-04-26 19:30:35 +00:00
addWordNounLookup()
2024-04-26 19:30:35 +00:00
addWordPhraseLookup()
2024-04-26 19:30:35 +00:00
addWordPrepositionLookup()
2024-04-26 19:30:35 +00:00
addWordPronounLookup()
2024-04-26 19:30:35 +00:00
addWordSentenceLookup()
2024-04-26 19:30:35 +00:00
addWordVerbLookup()
2024-04-26 19:30:35 +00:00
addWordCommentLookup()
2024-04-26 19:30:35 +00:00
addWordMiscLookup()
2024-04-26 19:30:35 +00:00
}
// internalFuncLookups is the internal map array with mapping to all available data
2024-04-26 19:30:35 +00:00
var internalFuncLookups map[string]Info = map[string]Info{
2024-04-26 19:30:35 +00:00
"fields": {
2024-04-26 19:30:35 +00:00
Description: "Example fields for generating csv, json, xml, etc",
Output: "gofakeit.Field",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
function, _ := GetRandomSimpleFunc(r)
2024-04-26 19:30:35 +00:00
return Field{
Name: function,
2024-04-26 19:30:35 +00:00
Function: function,
}, nil
2024-04-26 19:30:35 +00:00
},
},
}
// NewMapParams will create a new MapParams
2024-04-26 19:30:35 +00:00
func NewMapParams() *MapParams {
2024-04-26 19:30:35 +00:00
return &MapParams{}
2024-04-26 19:30:35 +00:00
}
// Add will take in a field and value and add it to the map params type
2024-04-26 19:30:35 +00:00
func (m *MapParams) Add(field string, value string) {
2024-04-26 19:30:35 +00:00
_, ok := (*m)[field]
2024-04-26 19:30:35 +00:00
if !ok {
2024-04-26 19:30:35 +00:00
(*m)[field] = []string{value}
2024-04-26 19:30:35 +00:00
return
2024-04-26 19:30:35 +00:00
}
(*m)[field] = append((*m)[field], value)
2024-04-26 19:30:35 +00:00
}
// Get will return the array of string from the provided field
2024-04-26 19:30:35 +00:00
func (m *MapParams) Get(field string) []string {
2024-04-26 19:30:35 +00:00
return (*m)[field]
2024-04-26 19:30:35 +00:00
}
// Size will return the total size of the underlying map
2024-04-26 19:30:35 +00:00
func (m *MapParams) Size() int {
2024-04-26 19:30:35 +00:00
size := 0
2024-04-26 19:30:35 +00:00
for range *m {
2024-04-26 19:30:35 +00:00
size++
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return size
2024-04-26 19:30:35 +00:00
}
// UnmarshalJSON will unmarshal the json into the []string
2024-04-26 19:30:35 +00:00
func (m *MapParamsValue) UnmarshalJSON(data []byte) error {
2024-04-26 19:30:35 +00:00
// check if the data is an array
2024-04-26 19:30:35 +00:00
// if so, marshal it into m
2024-04-26 19:30:35 +00:00
if data[0] == '[' {
2024-04-26 19:30:35 +00:00
var values []any
2024-04-26 19:30:35 +00:00
err := json.Unmarshal(data, &values)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
// convert the values to array of strings
2024-04-26 19:30:35 +00:00
for _, value := range values {
2024-04-26 19:30:35 +00:00
typeOf := reflect.TypeOf(value).Kind().String()
if typeOf == "map" {
2024-04-26 19:30:35 +00:00
v, err := json.Marshal(value)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
*m = append(*m, string(v))
2024-04-26 19:30:35 +00:00
} else {
2024-04-26 19:30:35 +00:00
*m = append(*m, fmt.Sprintf("%v", value))
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return nil
2024-04-26 19:30:35 +00:00
}
// if not, then convert into a string and add it to m
2024-04-26 19:30:35 +00:00
var s any
2024-04-26 19:30:35 +00:00
if err := json.Unmarshal(data, &s); err != nil {
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
*m = append(*m, fmt.Sprintf("%v", s))
2024-04-26 19:30:35 +00:00
return nil
2024-04-26 19:30:35 +00:00
}
func GetRandomSimpleFunc(r *rand.Rand) (string, Info) {
2024-04-26 19:30:35 +00:00
// Loop through all the functions and add them to a slice
2024-04-26 19:30:35 +00:00
var keys []string
2024-04-26 19:30:35 +00:00
for k, info := range FuncLookups {
2024-04-26 19:30:35 +00:00
// Only grab simple functions
2024-04-26 19:30:35 +00:00
if info.Params == nil {
2024-04-26 19:30:35 +00:00
keys = append(keys, k)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
// Randomly grab a function from the slice
2024-04-26 19:30:35 +00:00
randomKey := randomString(r, keys)
// Return the function name and info
2024-04-26 19:30:35 +00:00
return randomKey, FuncLookups[randomKey]
2024-04-26 19:30:35 +00:00
}
// AddFuncLookup takes a field and adds it to map
2024-04-26 19:30:35 +00:00
func AddFuncLookup(functionName string, info Info) {
2024-04-26 19:30:35 +00:00
if FuncLookups == nil {
2024-04-26 19:30:35 +00:00
FuncLookups = make(map[string]Info)
2024-04-26 19:30:35 +00:00
}
// Check content type
2024-04-26 19:30:35 +00:00
if info.ContentType == "" {
2024-04-26 19:30:35 +00:00
info.ContentType = "text/plain"
2024-04-26 19:30:35 +00:00
}
lockFuncLookups.Lock()
2024-04-26 19:30:35 +00:00
FuncLookups[functionName] = info
2024-04-26 19:30:35 +00:00
lockFuncLookups.Unlock()
2024-04-26 19:30:35 +00:00
}
// GetFuncLookup will lookup
2024-04-26 19:30:35 +00:00
func GetFuncLookup(functionName string) *Info {
2024-04-26 19:30:35 +00:00
var info Info
2024-04-26 19:30:35 +00:00
var ok bool
// Check internal functions first
2024-04-26 19:30:35 +00:00
info, ok = internalFuncLookups[functionName]
2024-04-26 19:30:35 +00:00
if ok {
2024-04-26 19:30:35 +00:00
return &info
2024-04-26 19:30:35 +00:00
}
info, ok = FuncLookups[functionName]
2024-04-26 19:30:35 +00:00
if ok {
2024-04-26 19:30:35 +00:00
return &info
2024-04-26 19:30:35 +00:00
}
return nil
2024-04-26 19:30:35 +00:00
}
// RemoveFuncLookup will remove a function from lookup
2024-04-26 19:30:35 +00:00
func RemoveFuncLookup(functionName string) {
2024-04-26 19:30:35 +00:00
_, ok := FuncLookups[functionName]
2024-04-26 19:30:35 +00:00
if !ok {
2024-04-26 19:30:35 +00:00
return
2024-04-26 19:30:35 +00:00
}
lockFuncLookups.Lock()
2024-04-26 19:30:35 +00:00
delete(FuncLookups, functionName)
2024-04-26 19:30:35 +00:00
lockFuncLookups.Unlock()
2024-04-26 19:30:35 +00:00
}
// GetAny will retrieve Any field from Info
2024-04-26 19:30:35 +00:00
func (i *Info) GetAny(m *MapParams, field string) (any, error) {
2024-04-26 19:30:35 +00:00
_, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
var anyValue any
// Try to convert to int
2024-04-26 19:30:35 +00:00
valueInt, err := strconv.ParseInt(value[0], 10, 64)
2024-04-26 19:30:35 +00:00
if err == nil {
2024-04-26 19:30:35 +00:00
return int(valueInt), nil
2024-04-26 19:30:35 +00:00
}
// Try to convert to float
2024-04-26 19:30:35 +00:00
valueFloat, err := strconv.ParseFloat(value[0], 64)
2024-04-26 19:30:35 +00:00
if err == nil {
2024-04-26 19:30:35 +00:00
return valueFloat, nil
2024-04-26 19:30:35 +00:00
}
// Try to convert to boolean
2024-04-26 19:30:35 +00:00
valueBool, err := strconv.ParseBool(value[0])
2024-04-26 19:30:35 +00:00
if err == nil {
2024-04-26 19:30:35 +00:00
return valueBool, nil
2024-04-26 19:30:35 +00:00
}
err = json.Unmarshal([]byte(value[0]), &anyValue)
2024-04-26 19:30:35 +00:00
if err == nil {
2024-04-26 19:30:35 +00:00
return valueBool, nil
2024-04-26 19:30:35 +00:00
}
return value[0], nil
2024-04-26 19:30:35 +00:00
}
// GetMap will retrieve map[string]any field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetMap(m *MapParams, field string) (map[string]any, error) {
2024-04-26 19:30:35 +00:00
_, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
var mapValue map[string]any
2024-04-26 19:30:35 +00:00
err = json.Unmarshal([]byte(value[0]), &mapValue)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, fmt.Errorf("%s field could not parse to map[string]any", field)
2024-04-26 19:30:35 +00:00
}
return mapValue, nil
2024-04-26 19:30:35 +00:00
}
// GetField will retrieve field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetField(m *MapParams, field string) (*Param, []string, error) {
2024-04-26 19:30:35 +00:00
// Get param
2024-04-26 19:30:35 +00:00
var p *Param
2024-04-26 19:30:35 +00:00
for _, param := range i.Params {
2024-04-26 19:30:35 +00:00
if param.Field == field {
2024-04-26 19:30:35 +00:00
p = &param
2024-04-26 19:30:35 +00:00
break
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if p == nil {
2024-04-26 19:30:35 +00:00
return nil, nil, fmt.Errorf("could not find param field %s", field)
2024-04-26 19:30:35 +00:00
}
// Get value from map
2024-04-26 19:30:35 +00:00
if m != nil {
2024-04-26 19:30:35 +00:00
value, ok := (*m)[field]
2024-04-26 19:30:35 +00:00
if !ok {
2024-04-26 19:30:35 +00:00
// If default isnt empty use default
2024-04-26 19:30:35 +00:00
if p.Default != "" {
2024-04-26 19:30:35 +00:00
return p, []string{p.Default}, nil
2024-04-26 19:30:35 +00:00
}
return nil, nil, fmt.Errorf("could not find field: %s", field)
2024-04-26 19:30:35 +00:00
}
return p, value, nil
2024-04-26 19:30:35 +00:00
} else if m == nil && p.Default != "" {
2024-04-26 19:30:35 +00:00
// If p.Type is []uint, then we need to convert it to []string
2024-04-26 19:30:35 +00:00
if strings.HasPrefix(p.Default, "[") {
2024-04-26 19:30:35 +00:00
// Remove [] from type
2024-04-26 19:30:35 +00:00
defaultClean := p.Default[1 : len(p.Default)-1]
// Split on comma
2024-04-26 19:30:35 +00:00
defaultSplit := strings.Split(defaultClean, ",")
return p, defaultSplit, nil
2024-04-26 19:30:35 +00:00
}
// If default isnt empty use default
2024-04-26 19:30:35 +00:00
return p, []string{p.Default}, nil
2024-04-26 19:30:35 +00:00
}
return nil, nil, fmt.Errorf("could not find field: %s", field)
2024-04-26 19:30:35 +00:00
}
// GetBool will retrieve boolean field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetBool(m *MapParams, field string) (bool, error) {
2024-04-26 19:30:35 +00:00
p, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return false, err
2024-04-26 19:30:35 +00:00
}
// Try to convert to boolean
2024-04-26 19:30:35 +00:00
valueBool, err := strconv.ParseBool(value[0])
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return false, fmt.Errorf("%s field could not parse to bool value", p.Field)
2024-04-26 19:30:35 +00:00
}
return valueBool, nil
2024-04-26 19:30:35 +00:00
}
// GetInt will retrieve int field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetInt(m *MapParams, field string) (int, error) {
2024-04-26 19:30:35 +00:00
p, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return 0, err
2024-04-26 19:30:35 +00:00
}
// Try to convert to int
2024-04-26 19:30:35 +00:00
valueInt, err := strconv.ParseInt(value[0], 10, 64)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return 0, fmt.Errorf("%s field could not parse to int value", p.Field)
2024-04-26 19:30:35 +00:00
}
return int(valueInt), nil
2024-04-26 19:30:35 +00:00
}
// GetUint will retrieve uint field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetUint(m *MapParams, field string) (uint, error) {
2024-04-26 19:30:35 +00:00
p, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return 0, err
2024-04-26 19:30:35 +00:00
}
// Try to convert to int
2024-04-26 19:30:35 +00:00
valueUint, err := strconv.ParseUint(value[0], 10, 64)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return 0, fmt.Errorf("%s field could not parse to int value", p.Field)
2024-04-26 19:30:35 +00:00
}
return uint(valueUint), nil
2024-04-26 19:30:35 +00:00
}
// GetFloat32 will retrieve int field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetFloat32(m *MapParams, field string) (float32, error) {
2024-04-26 19:30:35 +00:00
p, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return 0, err
2024-04-26 19:30:35 +00:00
}
// Try to convert to float
2024-04-26 19:30:35 +00:00
valueFloat, err := strconv.ParseFloat(value[0], 32)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return 0, fmt.Errorf("%s field could not parse to float value", p.Field)
2024-04-26 19:30:35 +00:00
}
return float32(valueFloat), nil
2024-04-26 19:30:35 +00:00
}
// GetFloat64 will retrieve int field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetFloat64(m *MapParams, field string) (float64, error) {
2024-04-26 19:30:35 +00:00
p, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return 0, err
2024-04-26 19:30:35 +00:00
}
// Try to convert to float
2024-04-26 19:30:35 +00:00
valueFloat, err := strconv.ParseFloat(value[0], 64)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return 0, fmt.Errorf("%s field could not parse to float value", p.Field)
2024-04-26 19:30:35 +00:00
}
return valueFloat, nil
2024-04-26 19:30:35 +00:00
}
// GetString will retrieve string field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetString(m *MapParams, field string) (string, error) {
2024-04-26 19:30:35 +00:00
_, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return "", err
2024-04-26 19:30:35 +00:00
}
return value[0], nil
2024-04-26 19:30:35 +00:00
}
// GetStringArray will retrieve []string field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetStringArray(m *MapParams, field string) ([]string, error) {
2024-04-26 19:30:35 +00:00
_, values, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
return values, nil
2024-04-26 19:30:35 +00:00
}
// GetIntArray will retrieve []int field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetIntArray(m *MapParams, field string) ([]int, error) {
2024-04-26 19:30:35 +00:00
_, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
var ints []int
2024-04-26 19:30:35 +00:00
for i := 0; i < len(value); i++ {
2024-04-26 19:30:35 +00:00
valueInt, err := strconv.ParseInt(value[i], 10, 64)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, fmt.Errorf("%s value could not parse to int", value[i])
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
ints = append(ints, int(valueInt))
2024-04-26 19:30:35 +00:00
}
return ints, nil
2024-04-26 19:30:35 +00:00
}
// GetUintArray will retrieve []uint field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetUintArray(m *MapParams, field string) ([]uint, error) {
2024-04-26 19:30:35 +00:00
_, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
var uints []uint
2024-04-26 19:30:35 +00:00
for i := 0; i < len(value); i++ {
2024-04-26 19:30:35 +00:00
valueUint, err := strconv.ParseUint(value[i], 10, 64)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, fmt.Errorf("%s value could not parse to uint", value[i])
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
uints = append(uints, uint(valueUint))
2024-04-26 19:30:35 +00:00
}
return uints, nil
2024-04-26 19:30:35 +00:00
}
// GetFloat32Array will retrieve []float field from data
2024-04-26 19:30:35 +00:00
func (i *Info) GetFloat32Array(m *MapParams, field string) ([]float32, error) {
2024-04-26 19:30:35 +00:00
_, value, err := i.GetField(m, field)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
var floats []float32
2024-04-26 19:30:35 +00:00
for i := 0; i < len(value); i++ {
2024-04-26 19:30:35 +00:00
valueFloat, err := strconv.ParseFloat(value[i], 32)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, fmt.Errorf("%s value could not parse to float", value[i])
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
floats = append(floats, float32(valueFloat))
2024-04-26 19:30:35 +00:00
}
return floats, nil
2024-04-26 19:30:35 +00:00
}