/
ccf_type_id.go
105 lines (87 loc) · 2.65 KB
/
ccf_type_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
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ccf
import (
"fmt"
"math/big"
"github.com/onflow/cadence"
)
// ccfTypeID represents CCF type ID.
type ccfTypeID uint64
func newCCFTypeID(b []byte) ccfTypeID {
return ccfTypeID(new(big.Int).SetBytes(b).Uint64())
}
func newCCFTypeIDFromUint64(i uint64) ccfTypeID {
return ccfTypeID(i)
}
func (id ccfTypeID) Bytes() []byte {
return new(big.Int).SetUint64(uint64(id)).Bytes()
}
func (id ccfTypeID) Equal(other ccfTypeID) bool {
return id == other
}
// ccfTypeIDByCadenceType maps a Cadence type ID to a CCF type ID
//
// IMPORTANT: Don't use cadence.Type as map key because all Cadence composite/interface
// types are pointers, and different instance of the same type will be treated as
// different map key.
type ccfTypeIDByCadenceType map[string]ccfTypeID
func (types ccfTypeIDByCadenceType) id(t cadence.Type) (ccfTypeID, error) {
id, ok := types[t.ID()]
if !ok {
return 0, fmt.Errorf("CCF type ID not found for type %s", t.ID())
}
return id, nil
}
type cadenceTypeByCCFTypeID struct {
types map[ccfTypeID]cadence.Type
referencedTypes map[ccfTypeID]struct{}
}
func newCadenceTypeByCCFTypeID() *cadenceTypeByCCFTypeID {
return &cadenceTypeByCCFTypeID{
types: make(map[ccfTypeID]cadence.Type),
referencedTypes: make(map[ccfTypeID]struct{}),
}
}
func (ids *cadenceTypeByCCFTypeID) add(id ccfTypeID, typ cadence.Type) bool {
if ids.has(id) {
return false
}
ids.types[id] = typ
return true
}
func (ids *cadenceTypeByCCFTypeID) reference(id ccfTypeID) {
ids.referencedTypes[id] = struct{}{}
}
func (ids *cadenceTypeByCCFTypeID) typ(id ccfTypeID) (cadence.Type, error) {
t, ok := ids.types[id]
if !ok {
return nil, fmt.Errorf("type not found for CCF type ID %d", id)
}
return t, nil
}
func (ids *cadenceTypeByCCFTypeID) has(id ccfTypeID) bool {
_, ok := ids.types[id]
return ok
}
func (ids *cadenceTypeByCCFTypeID) count() int {
return len(ids.types)
}
func (ids *cadenceTypeByCCFTypeID) hasUnreferenced() bool {
return len(ids.types) > len(ids.referencedTypes)
}