2024-08-09 15:00:52 +00:00
|
|
|
package builder
|
2024-08-09 14:14:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-08-09 22:33:10 +00:00
|
|
|
func convertFields(fields []Map) (string, error) {
|
2024-08-09 14:14:42 +00:00
|
|
|
var expressions []string
|
|
|
|
for _, fieldAssoc := range fields {
|
2024-08-09 22:33:10 +00:00
|
|
|
keys := aggregateSortedKeys([]Map{fieldAssoc})
|
|
|
|
for _, key := range keys {
|
|
|
|
field := key
|
|
|
|
as := fieldAssoc[key]
|
2024-08-09 14:14:42 +00:00
|
|
|
asBool, ok := as.(bool)
|
|
|
|
if ok {
|
|
|
|
if asBool {
|
|
|
|
expressions = append(expressions, field)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
asNum, ok := as.(int)
|
2024-08-15 04:23:36 +00:00
|
|
|
if !ok {
|
|
|
|
n, k := as.(int64)
|
|
|
|
asNum = int(n)
|
|
|
|
ok = k
|
|
|
|
}
|
2024-08-09 14:14:42 +00:00
|
|
|
if ok {
|
|
|
|
if asNum == 1 {
|
|
|
|
expressions = append(expressions, field)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
asStr, ok := as.(string)
|
|
|
|
if ok {
|
|
|
|
expressions = append(expressions, fmt.Sprintf("%s AS %s", field, asStr))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("invalid field value: %v", as)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(expressions, ", "), nil
|
|
|
|
}
|