Slide 50
Slide 50 text
@jrbowes
// MarshalText marshals a Verb to a string, for use in JSON. Verbs are
// stored in raw base64 url (no trailing ==), with leading zero bytes
// removed. This allows us to both store sparse Verbs efficiently, and
// have forwards compatibility as additional verbs are added, growing
// the bit length.
func (v Verb) MarshalText() ([]byte, error) {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
for len(b) > 0 && b[0] == 0x00 {
b = b[1:]
}
o := make([]byte, base64.RawURLEncoding.EncodedLen(len(b)))
base64.RawURLEncoding.Encode(o, b)
return o, nil
}