Use custom identifiers
This commit is contained in:
parent
b2d50972ed
commit
299f3ba0cf
5 changed files with 45 additions and 16 deletions
31
model/identifier.go
Normal file
31
model/identifier.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue