-
Notifications
You must be signed in to change notification settings - Fork 10
/
types.go
129 lines (105 loc) · 3.99 KB
/
types.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cedar
import (
"time"
"github.com/cedar-policy/cedar-go/internal/mapset"
"github.com/cedar-policy/cedar-go/types"
"golang.org/x/exp/constraints"
)
// _____
// |_ _| _ _ __ ___ ___
// | || | | | '_ \ / _ \/ __|
// | || |_| | |_) | __/\__ \
// |_| \__, | .__/ \___||___/
// |___/|_|
// Cedar data types
type Boolean = types.Boolean
type Datetime = types.Datetime
type Decimal = types.Decimal
type Duration = types.Duration
type EntityUID = types.EntityUID
type IPAddr = types.IPAddr
type Long = types.Long
type Record = types.Record
type RecordMap = types.RecordMap
type Set = types.Set
type String = types.String
// Other Cedar types
type EntityMap = types.EntityMap
type Entity = types.Entity
type EntityType = types.EntityType
type EntityUIDSet = types.EntityUIDSet
type Pattern = types.Pattern
type Wildcard = types.Wildcard
// cedar-go types
type Value = types.Value
// ____ _ _
// / ___|___ _ __ ___| |_ __ _ _ __ | |_ ___
// | | / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
// | |__| (_) | | | \__ \ || (_| | | | | |_\__ \
// \____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
const (
True = types.True
False = types.False
)
// ____ _ _
// / ___|___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __ ___
// | | / _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__/ __|
// | |__| (_) | | | \__ \ |_| | | |_| | (__| || (_) | | \__ \
// \____\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_| |___/
// DatetimeFromMillis returns a Datetime from milliseconds
func DatetimeFromMillis(ms int64) Datetime {
return types.DatetimeFromMillis(ms)
}
// DurationFromMillis returns a Duration from milliseconds
func DurationFromMillis(ms int64) Duration {
return types.DurationFromMillis(ms)
}
// FromStdDuration returns a Cedar Duration from a Go time.Duration
func FromStdDuration(d time.Duration) Duration {
return types.FromStdDuration(d)
}
// FromStdTime returns a Cedar Datetime from a Go time.Time value
func FromStdTime(t time.Time) Datetime {
return types.FromStdTime(t)
}
// NewEntityUID returns an EntityUID given an EntityType and identifier
func NewEntityUID(typ EntityType, id String) EntityUID {
return types.NewEntityUID(typ, id)
}
// NewEntityUIDSet returns an immutable EntityUIDSet ready for use.
func NewEntityUIDSet(args ...EntityUID) EntityUIDSet {
return mapset.Immutable[EntityUID](args...)
}
// NewPattern permits for the programmatic construction of a Pattern out of a slice of pattern components.
// The pattern components may be one of string, cedar.String, or cedar.Wildcard. Any other types will
// cause a panic.
func NewPattern(components ...any) Pattern {
return types.NewPattern(components)
}
// NewRecord returns an immutable Record given a Go map of Strings to Values
func NewRecord(r RecordMap) Record {
return types.NewRecord(r)
}
// NewSet returns an immutable Set given a variadic set of Values. Duplicates are removed and order is not preserved.
func NewSet(s ...types.Value) Set {
return types.NewSet(s...)
}
// NewDecimal returns a Decimal value of i * 10^exponent.
func NewDecimal(i int64, exponent int) (Decimal, error) {
return types.NewDecimal(i, exponent)
}
// NewDecimalFromInt returns a Decimal with the whole integer value provided
func NewDecimalFromInt[T constraints.Signed](i T) (Decimal, error) {
return types.NewDecimalFromInt(i)
}
// NewDecimalFromFloat returns a Decimal that approximates the given floating point value.
// The value of the Decimal is calculated by multiplying it by 10^4, truncating it to
// an int64 representation to cut off any digits beyond the four allowed, and passing it
// as an integer to NewDecimal() with -4 as the exponent.
//
// WARNING: decimal representations of more than 6 significant digits for float32s and 15
// significant digits for float64s can be lossy in terms of precision. To create a precise
// Decimal above those sizes, use the NewDecimal constructor.
func NewDecimalFromFloat[T constraints.Float](f T) (Decimal, error) {
return types.NewDecimalFromFloat(f)
}