-
Notifications
You must be signed in to change notification settings - Fork 3
/
var_string.go
66 lines (56 loc) · 1.85 KB
/
var_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
package coltype
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/go-sif/sif"
)
// varStringType is a ColumnType which stores a variable-length string value
type varStringType struct {
// TODO store encoding type via https://godoc.org/golang.org/x/text/encoding, for inter-language stringing
}
// ToString produces a string representation of a value of a varStringType value
func (c *varStringType) ToStringT(v string) string {
return fmt.Sprintf("\"%s\"", v)
}
// ToString produces a string representation of a value of a varStringType value
func (c *varStringType) ToString(v interface{}) string {
return c.ToStringT(v.(string))
}
// Serialize defines how values of this type are serialized into raw bytes
func (c *varStringType) SerializeT(v string) ([]byte, error) {
buff := new(bytes.Buffer)
e := gob.NewEncoder(buff)
err := e.Encode(v)
if err != nil {
return nil, err
}
return buff.Bytes(), nil
}
// Serialize defines how values of this type are serialized into raw bytes
func (c *varStringType) Serialize(v interface{}) ([]byte, error) {
vs, ok := v.(string)
if !ok {
return nil, fmt.Errorf("value %v is not a string", v)
}
return c.SerializeT(vs)
}
// Deserialize defines how values of this type are deserialized from raw bytes
func (c *varStringType) DeserializeT(sv []byte) (string, error) {
var v string
buff := bytes.NewBuffer(sv)
d := gob.NewDecoder(buff)
err := d.Decode(&v)
if err != nil {
return "", err
}
return v, nil
}
// Deserialize defines how values of this type are deserialized from raw bytes
func (c *varStringType) Deserialize(sv []byte) (interface{}, error) {
return c.DeserializeT(sv)
}
// VarString returns a GenericColumnAccessor for a ColumnType which stores a variable-length string value
func VarString(colName string) sif.GenericColumnAccessor[string] {
return sif.CreateColumnAccessor[string](&varStringType{}, colName)
}