/
data_id.go
110 lines (98 loc) · 2.78 KB
/
data_id.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors
// SPDX-License-Identifier: Apache-2.0
package base
import (
"bytes"
"fmt"
"github.com/AssetMantle/schema/go/data"
"github.com/AssetMantle/schema/go/ids"
"github.com/AssetMantle/schema/go/ids/constants"
stringUtilities "github.com/AssetMantle/schema/go/ids/utilities"
"strings"
)
var _ ids.DataID = (*DataID)(nil)
func (dataID *DataID) ValidateBasic() error {
if dataID == nil {
return fmt.Errorf("data ID is empty")
}
if err := dataID.TypeID.ValidateBasic(); err != nil {
return err
}
if err := dataID.HashID.ValidateBasic(); err != nil {
return err
}
return nil
}
func (dataID *DataID) GetTypeID() ids.StringID {
return NewStringID(constants.DataIDType)
}
func (dataID *DataID) FromString(idString string) (ids.ID, error) {
idString = strings.TrimSpace(idString)
if idString == "" {
return PrototypeDataID(), nil
}
typeIDAndHashID := stringUtilities.SplitCompositeIDString(idString)
if len(typeIDAndHashID) != 2 {
return PrototypeDataID(), fmt.Errorf("invalid dataID string %s", idString)
} else if typeID, err := PrototypeStringID().FromString(typeIDAndHashID[0]); err != nil {
return PrototypeDataID(), err
} else if hashID, err := PrototypeHashID().FromString(typeIDAndHashID[1]); err != nil {
return PrototypeDataID(), err
} else {
dataID := &DataID{TypeID: typeID.(*StringID), HashID: hashID.(*HashID)}
if dataID.ValidateBasic() != nil {
return PrototypeDataID(), err
}
return dataID, nil
}
}
func (dataID *DataID) AsString() string {
return stringUtilities.JoinIDStrings(dataID.TypeID.AsString(), dataID.HashID.AsString())
}
func (dataID *DataID) GetHashID() ids.HashID {
return dataID.HashID
}
func (dataID *DataID) IsDataID() {
}
func (dataID *DataID) DataIDString() string {
return stringUtilities.JoinIDStrings(dataID.TypeID.AsString(), dataID.HashID.AsString())
}
func (dataID *DataID) Bytes() []byte {
var Bytes []byte
Bytes = append(Bytes, dataID.TypeID.Bytes()...)
Bytes = append(Bytes, dataID.HashID.Bytes()...)
return Bytes
}
func (dataID *DataID) Compare(id ids.ID) int {
return bytes.Compare(dataID.Bytes(), id.ToAnyID().Get().(*DataID).Bytes())
}
func (dataID *DataID) ToAnyID() ids.AnyID {
return &AnyID{
Impl: &AnyID_DataID{
DataID: dataID,
},
}
}
func dataIDFromInterface(i interface{}) *DataID {
switch value := i.(type) {
case *DataID:
return value
default:
panic(fmt.Errorf("expected *DataID, got %T", i))
}
}
func GenerateDataID(data data.Data) ids.DataID {
if data == nil {
panic(fmt.Errorf("data is nil"))
}
return &DataID{
TypeID: data.GetTypeID().(*StringID),
HashID: data.GenerateHashID().(*HashID),
}
}
func PrototypeDataID() ids.DataID {
return &DataID{
TypeID: PrototypeStringID().(*StringID),
HashID: PrototypeHashID().(*HashID),
}
}