32 lines
748 B
Go
32 lines
748 B
Go
|
package reveil
|
||
|
|
||
|
import (
|
||
|
"encoding/base64"
|
||
|
"errors"
|
||
|
)
|
||
|
|
||
|
type Identifier []byte
|
||
|
|
||
|
func (i *Identifier) ToString() string {
|
||
|
return base64.RawURLEncoding.EncodeToString(*i)
|
||
|
}
|
||
|
|
||
|
func (i *Identifier) MarshalJSON() (dst []byte, err error) {
|
||
|
dst = make([]byte, base64.RawURLEncoding.EncodedLen(len(*i)))
|
||
|
base64.RawURLEncoding.Encode(dst, *i)
|
||
|
dst = append([]byte{'"'}, dst...)
|
||
|
dst = append(dst, '"')
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (i *Identifier) UnmarshalJSON(src []byte) error {
|
||
|
if len(src) < 2 || src[0] != '"' || src[len(src)-1] != '"' {
|
||
|
return errors.New("Unvalid character found to encapsulate the JSON value")
|
||
|
}
|
||
|
|
||
|
*i = make([]byte, base64.RawURLEncoding.DecodedLen(len(src)-2))
|
||
|
_, err := base64.RawURLEncoding.Decode(*i, src[1:len(src)-1])
|
||
|
|
||
|
return err
|
||
|
}
|