-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathitem.go
70 lines (61 loc) · 1.4 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ownership
import (
"strings"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/logger"
)
// OwnedItem - the flag byte
type OwnedItem byte
// type codes for flag byte
const (
OwnedAsset OwnedItem = iota
OwnedBlock OwnedItem = iota
OwnedShare OwnedItem = iota
)
// internal conversion
func toString(item OwnedItem) ([]byte, error) {
switch item {
case OwnedAsset:
return []byte("Asset"), nil
case OwnedBlock:
return []byte("Block"), nil
case OwnedShare:
return []byte("Share"), nil
default:
return []byte{}, fault.InvalidItem
}
}
// String - convert a owned item to its string symbol
func (item OwnedItem) String() string {
s, err := toString(item)
if err != nil {
logger.Panicf("invalid item enumeration: %d", item)
}
return string(s)
}
// MarshalText - convert item to text
func (item OwnedItem) MarshalText() ([]byte, error) {
s, err := toString(item)
if err != nil {
return nil, err
}
return s, nil
}
// UnmarshalText - convert test to Item
func (item *OwnedItem) UnmarshalText(s []byte) error {
switch strings.ToLower(string(s)) {
case "asset":
*item = OwnedAsset
case "block":
*item = OwnedBlock
case "share":
*item = OwnedShare
default:
return fault.NotOwnedItem
}
return nil
}