-
Notifications
You must be signed in to change notification settings - Fork 153
/
package.go
165 lines (152 loc) · 4.03 KB
/
package.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package interpreter
import (
"fmt"
"regexp"
"strings"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
// Package is an importable package that can be used from another
// section of code. The package itself cannot have its attributes
// modified after creation, but the options may be changed.
type Package struct {
// name is the name of the package.
name string
// path is the canonical import path that is used to import this package.
path string
// object contains the object properties of this package.
object values.Object
// sideEffects contains the side effects caused by this package.
// This is currently unused.
sideEffects []SideEffect
}
func NewPackageWithValues(name, path string, obj values.Object) *Package {
return &Package{
name: name,
path: path,
object: obj,
}
}
func NewPackage(name string) *Package {
obj := values.NewObject(semantic.NewObjectType(nil))
return NewPackageWithValues(name, "", obj)
}
func (p *Package) Copy() *Package {
var sideEffects []SideEffect
if len(p.sideEffects) > 0 {
sideEffects = make([]SideEffect, len(p.sideEffects))
copy(sideEffects, p.sideEffects)
}
return &Package{
name: p.name,
object: p.object,
sideEffects: sideEffects,
}
}
// Name returns the package name.
func (p *Package) Name() string {
return p.name
}
// Path returns the canonical import path for this package.
func (p *Package) Path() string {
return p.path
}
func (p *Package) SideEffects() []SideEffect {
return p.sideEffects
}
func (p *Package) Type() semantic.MonoType {
return p.object.Type()
}
func (p *Package) Get(name string) (values.Value, bool) {
return p.object.Get(name)
}
func (p *Package) Set(name string, v values.Value) {
panic(errors.New(codes.Internal, "package members cannot be modified"))
}
func (p *Package) Len() int {
return p.object.Len()
}
func (p *Package) Range(f func(name string, v values.Value)) {
p.object.Range(f)
}
func (p *Package) IsNull() bool {
return false
}
func (p *Package) Str() string {
panic(values.UnexpectedKind(semantic.Object, semantic.String))
}
func (p *Package) Bytes() []byte {
panic(values.UnexpectedKind(semantic.Object, semantic.Bytes))
}
func (p *Package) Int() int64 {
panic(values.UnexpectedKind(semantic.Object, semantic.Int))
}
func (p *Package) UInt() uint64 {
panic(values.UnexpectedKind(semantic.Object, semantic.UInt))
}
func (p *Package) Float() float64 {
panic(values.UnexpectedKind(semantic.Object, semantic.Float))
}
func (p *Package) Bool() bool {
panic(values.UnexpectedKind(semantic.Object, semantic.Bool))
}
func (p *Package) Time() values.Time {
panic(values.UnexpectedKind(semantic.Object, semantic.Time))
}
func (p *Package) Duration() values.Duration {
panic(values.UnexpectedKind(semantic.Object, semantic.Duration))
}
func (p *Package) Regexp() *regexp.Regexp {
panic(values.UnexpectedKind(semantic.Object, semantic.Regexp))
}
func (p *Package) Array() values.Array {
panic(values.UnexpectedKind(semantic.Object, semantic.Array))
}
func (p *Package) Object() values.Object {
return p
}
func (p *Package) Function() values.Function {
panic(values.UnexpectedKind(semantic.Object, semantic.Function))
}
func (p *Package) Dict() values.Dictionary {
panic(values.UnexpectedKind(semantic.Object, semantic.Dictionary))
}
func (p *Package) Equal(rhs values.Value) bool {
if p.Type() != rhs.Type() {
return false
}
r := rhs.Object()
if p.Len() != r.Len() {
return false
}
equal := true
p.Range(func(k string, v values.Value) {
if !equal {
return
}
val, ok := r.Get(k)
equal = ok && v.Equal(val)
})
return equal
}
func (p *Package) String() string {
var builder strings.Builder
builder.WriteString("pkg{")
i := 0
p.Range(func(k string, v values.Value) {
if _, ok := v.(*Package); ok {
return
}
if i != 0 {
builder.WriteString(", ")
}
builder.WriteString(k)
builder.WriteString(": ")
builder.WriteString(fmt.Sprintf("%v", v.Type()))
i++
})
builder.WriteRune('}')
return builder.String()
}