2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-14 09:14:07 +00:00
restic/id.go

61 lines
1.0 KiB
Go
Raw Normal View History

2014-07-28 18:20:32 +00:00
package khepri
2014-04-21 21:25:31 +00:00
import (
"bytes"
"encoding/hex"
"encoding/json"
)
// References content within a repository.
type ID []byte
// ParseID converts the given string to an ID.
func ParseID(s string) (ID, error) {
b, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
return ID(b), nil
}
2014-07-28 18:20:32 +00:00
2014-04-21 21:25:31 +00:00
func (id ID) String() string {
return hex.EncodeToString(id)
}
// Equal compares an ID to another other.
func (id ID) Equal(other ID) bool {
return bytes.Equal(id, other)
}
// EqualString compares this ID to another one, given as a string.
func (id ID) EqualString(other string) (bool, error) {
s, err := hex.DecodeString(other)
if err != nil {
return false, err
}
return id.Equal(ID(s)), nil
}
func (id ID) MarshalJSON() ([]byte, error) {
return json.Marshal(id.String())
}
func (id *ID) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*id = make([]byte, len(s)/2)
_, err = hex.Decode(*id, []byte(s))
if err != nil {
return err
}
return nil
}