package value
import (
"encoding/binary"
"math"
)
const (
tagNull byte = iota
tagBool
tagInt
tagFloat
tagStr
tagList
tagNode
tagRel
tagPath
tagMap
tagTemporal
tagDuration
)
func Key (v Value ) string { return string (AppendKey (nil , v )) }
func AppendKey (b []byte , v Value ) []byte {
switch v .kind {
case KindNull :
return append (b , tagNull )
case KindBool :
return append (b , tagBool , byte (v .num ))
case KindInt :
return appendU64 (append (b , tagInt ), v .num )
case KindFloat :
return appendFloatKey (b , math .Float64frombits (v .num ))
case KindStr :
b = appendLen (append (b , tagStr ), len (v .str ))
return append (b , v .str ...)
case KindList :
b = appendLen (append (b , tagList ), len (v .ext .list ))
for _ , e := range v .ext .list {
b = AppendKey (b , e )
}
return b
case KindNode :
return appendU32 (append (b , tagNode ), uint32 (v .num ))
case KindRel :
return appendU32 (append (b , tagRel ), uint32 (v .num ))
case KindPath :
b = appendLen (append (b , tagPath ), len (v .ext .nodes ))
for _ , n := range v .ext .nodes {
b = appendU32 (b , uint32 (n ))
}
b = appendLen (b , len (v .ext .rels ))
for _ , r := range v .ext .rels {
b = appendU32 (b , r )
}
return b
case KindMap :
b = appendLen (append (b , tagMap ), len (v .ext .entries ))
for _ , i := range keySorted (v .ext .entries ) {
e := v .ext .entries [i ]
b = appendLen (b , len (e .Key ))
b = append (b , e .Key ...)
b = AppendKey (b , e .Val )
}
return b
case KindTemporal :
return append (appendU64 (append (b , tagTemporal ), v .num ), v .aux )
case KindDuration :
b = appendU64 (append (b , tagDuration ), uint64 (v .ext .months ))
b = appendU64 (b , uint64 (v .ext .days ))
return appendU64 (b , v .num )
}
return append (b , tagNull )
}
func appendFloatKey(b []byte , f float64 ) []byte {
if f == math .Trunc (f ) && f >= math .MinInt64 && f < 9223372036854775808.0 {
return appendU64 (append (b , tagInt ), uint64 (int64 (f )))
}
return appendU64 (append (b , tagFloat ), math .Float64bits (f ))
}
func appendU64(b []byte , v uint64 ) []byte { return binary .BigEndian .AppendUint64 (b , v ) }
func appendU32(b []byte , v uint32 ) []byte { return binary .BigEndian .AppendUint32 (b , v ) }
func appendLen(b []byte , n int ) []byte { return appendU32 (b , uint32 (n )) }
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .