-
Notifications
You must be signed in to change notification settings - Fork 11
/
maps.go
51 lines (46 loc) · 1.07 KB
/
maps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright (c) 2020-present unTill Pro, Ltd.
* @author Denis Gribanov
*/
package coreutils
import (
"errors"
"fmt"
"strings"
"github.com/voedger/voedger/pkg/istructs"
)
// converts slice of "k=v" to map[k]v
func PairsToMap(pairs []string, m map[string]string) error {
for _, pair := range pairs {
vals := strings.Split(pair, "=")
if len(vals) != 2 {
return errors.New("wrong pair value: " + pair)
}
m[vals[0]] = vals[1]
}
return nil
}
func MapToObject(data map[string]interface{}, rw istructs.IRowWriter) (err error) {
for fieldName, vIntf := range data {
switch v := vIntf.(type) {
case nil:
case float64:
rw.PutNumber(fieldName, v)
case float32:
rw.PutNumber(fieldName, float64(v))
case int32:
rw.PutNumber(fieldName, float64(v))
case int64:
rw.PutNumber(fieldName, float64(v))
case istructs.RecordID:
rw.PutNumber(fieldName, float64(v))
case string:
rw.PutChars(fieldName, v)
case bool:
rw.PutBool(fieldName, v)
default:
return fmt.Errorf("field %s: unsupported value type %#v", fieldName, vIntf)
}
}
return nil
}