-
Notifications
You must be signed in to change notification settings - Fork 39
/
guid.go
71 lines (64 loc) · 1.52 KB
/
guid.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
70
71
package value
import (
"fmt"
"reflect"
"github.com/google/uuid"
)
// GUID represents a Kusto GUID type. GUID implements Kusto.
type GUID struct {
// Value holds the value of the type.
Value uuid.UUID
// Valid indicates if this value was set.
Valid bool
}
func (GUID) isKustoVal() {}
// String implements fmt.Stringer.
func (g GUID) String() string {
if !g.Valid {
return ""
}
return g.Value.String()
}
// Unmarshal unmarshals i into GUID. i must be a string representing a GUID or nil.
func (g *GUID) Unmarshal(i interface{}) error {
if i == nil {
g.Value = uuid.UUID{}
g.Valid = false
return nil
}
str, ok := i.(string)
if !ok {
return fmt.Errorf("Column with type 'guid' was not stored as a string, was %T", i)
}
u, err := uuid.Parse(str)
if err != nil {
return fmt.Errorf("Column with type 'guid' did not store a valid uuid(%s): %s", str, err)
}
g.Value = u
g.Valid = true
return nil
}
// Convert GUID into reflect value.
func (g GUID) Convert(v reflect.Value) error {
t := v.Type()
switch {
case t.AssignableTo(reflect.TypeOf(uuid.UUID{})):
if g.Valid {
v.Set(reflect.ValueOf(g.Value))
}
return nil
case t.ConvertibleTo(reflect.TypeOf(new(uuid.UUID))):
if g.Valid {
t := &g.Value
v.Set(reflect.ValueOf(t))
}
return nil
case t.ConvertibleTo(reflect.TypeOf(GUID{})):
v.Set(reflect.ValueOf(g))
return nil
case t.ConvertibleTo(reflect.TypeOf(&GUID{})):
v.Set(reflect.ValueOf(&g))
return nil
}
return fmt.Errorf("Column was type Kusto.GUID, receiver had base Kind %s ", t.Kind())
}