-
Notifications
You must be signed in to change notification settings - Fork 41
/
string.go
67 lines (59 loc) · 1.31 KB
/
string.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
package value
import (
"fmt"
"reflect"
)
// String represents a Kusto string type. String implements Kusto.
type String struct {
// Value holds the value of the type.
Value string
// Valid indicates if this value was set.
Valid bool
}
func (String) isKustoVal() {}
// String implements fmt.Stringer.
func (s String) String() string {
if !s.Valid {
return ""
}
return s.Value
}
// Unmarshal unmarshals i into String. i must be a string or nil.
func (s *String) Unmarshal(i interface{}) error {
if i == nil {
s.Value = ""
s.Valid = false
return nil
}
v, ok := i.(string)
if !ok {
return fmt.Errorf("Column with type 'string' had type %T", i)
}
s.Value = v
s.Valid = true
return nil
}
// Convert String into reflect value.
func (s String) Convert(v reflect.Value) error {
t := v.Type()
switch {
case t.Kind() == reflect.String:
if s.Valid {
v.Set(reflect.ValueOf(s.Value))
}
return nil
case t.ConvertibleTo(reflect.TypeOf(new(string))):
if s.Valid {
i := &s.Value
v.Set(reflect.ValueOf(i))
}
return nil
case t.ConvertibleTo(reflect.TypeOf(String{})):
v.Set(reflect.ValueOf(s))
return nil
case t.ConvertibleTo(reflect.TypeOf(&String{})):
v.Set(reflect.ValueOf(&s))
return nil
}
return fmt.Errorf("Column was type Kusto.String, receiver had base Kind %s ", t.Kind())
}