forked from viant/dsunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.go
69 lines (59 loc) · 1.47 KB
/
record.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package dsunit
import (
"fmt"
"github.com/viant/toolbox"
"strings"
)
type Record map[string]interface{}
//Value returns raw column value for this row.
func (r *Record) Value(column string) interface{} {
if value, ok := (*r)[column]; ok {
return value
}
return nil
}
//ValueAsString returns column value as string.
func (r Record) ValueAsString(column string) string {
return fmt.Sprintf("%v", r.Value(column))
}
//IsEmpty return true if empty
func (r *Record) IsEmpty() bool {
return len(r.Columns()) == 0
}
//Columns returns column names.
func (r *Record) Columns() []string {
var result = make([]string, 0)
for k := range *r {
if k == "" || strings.HasPrefix(k, "@") && strings.Count(k, "@") > 1 {
continue
}
result = append(result, k)
}
return result
}
//SetValue sets column value on this row.
func (r *Record) SetValue(column string, value interface{}) {
(*r)[column] = value
}
//HasColumn returns true if this row has passed in column value.
func (r *Record) HasColumn(column string) bool {
if _, ok := (*r)[column]; ok {
return true
}
return false
}
//AsMAp returns record as map
func (r *Record) AsMap() map[string]interface{} {
var result = make(map[string]interface{})
for _, column := range r.Columns() {
result[column] = r.Value(column)
}
return result
}
//String return row content as string JSON.
func (r *Record) String() string {
if result, err := toolbox.AsJSONText(r.AsMap()); err == nil {
return result
}
return fmt.Sprintf("%v", (*r))
}