-
Notifications
You must be signed in to change notification settings - Fork 42
/
string.go
64 lines (51 loc) · 1.43 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
package data
import (
"encoding/json"
"fmt"
"time"
)
// String is a string. It can be assigned to Value interface.
type String string
// Type returns TypeID of String. It's always TypeString.
func (s String) Type() TypeID {
return TypeString
}
func (s String) asBool() (bool, error) {
return false, castError(s.Type(), TypeBool)
}
func (s String) asInt() (int64, error) {
return 0, castError(s.Type(), TypeInt)
}
func (s String) asFloat() (float64, error) {
return 0, castError(s.Type(), TypeFloat)
}
func (s String) asString() (string, error) {
return string(s), nil
}
func (s String) asBlob() ([]byte, error) {
return nil, castError(s.Type(), TypeBlob)
}
func (s String) asTimestamp() (time.Time, error) {
return time.Time{}, castError(s.Type(), TypeTimestamp)
}
func (s String) asArray() (Array, error) {
return nil, castError(s.Type(), TypeArray)
}
func (s String) asMap() (Map, error) {
return nil, castError(s.Type(), TypeMap)
}
func (s String) clone() Value {
return String(s)
}
// String returns JSON representation of a String. A string "a" will be marshaled
// as `"a"` (double quotes are included), not `a`. To obtain a plain string
// without double quotes, use ToString function.
func (s String) String() string {
// the String return value is defined via the
// default JSON serialization
bytes, err := json.Marshal(s)
if err != nil {
return fmt.Sprintf("(unserializable string: %v)", err)
}
return string(bytes)
}