File: utils.go

package info (click to toggle)
incus 6.0.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,788 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (246 lines) | stat: -rw-r--r-- 6,204 bytes parent folder | download | duplicates (2)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package scriptlet

import (
	"errors"
	"fmt"
	"slices"
	"sort"
	"strings"
	"sync"

	"go.starlark.net/starlark"
	"go.starlark.net/syntax"
)

// Loader holds the programs for the scriptlet.
type Loader struct {
	programsMu sync.Mutex
	programs   map[string]*starlark.Program
}

// argMismatch represents mismatching arguments in a function.
type argMismatch struct {
	gotten   []string
	expected []string
}

// scriptletFunction represents a possibly optional function in a scriptlet.
type scriptletFunction struct {
	name     string
	optional bool
}

// Declaration is a type alias to make scriptlet declaration easier.
type Declaration = map[scriptletFunction][]string

// NewLoader creates a new Loader.
func NewLoader() *Loader {
	return &Loader{
		programsMu: sync.Mutex{},
		programs:   map[string]*starlark.Program{},
	}
}

// Compile compiles a scriptlet.
func Compile(programName string, src string, preDeclared []string) (*starlark.Program, error) {
	isPreDeclared := func(name string) bool {
		return slices.Contains(preDeclared, name)
	}

	// Prepare options.
	opts := syntax.LegacyFileOptions()
	opts.Set = true

	// Parse, resolve, and compile a Starlark source file.
	_, mod, err := starlark.SourceProgramOptions(opts, programName, src, isPreDeclared)
	if err != nil {
		return nil, err
	}

	return mod, nil
}

// Required is a convenience wrapper declaring a required function.
func Required(name string) scriptletFunction {
	return scriptletFunction{name: name, optional: false}
}

// Optional is a convenience wrapper declaring an optional function.
func Optional(name string) scriptletFunction {
	return scriptletFunction{name: name, optional: true}
}

// optionalToString converts a Boolean describing optional functions to its string representation.
func optionalToString(optional bool) string {
	if optional {
		return "optional"
	}

	return "required"
}

// validateFunction validates a single Starlark function.
func validateFunction(funv starlark.Value, requiredArgs []string) (bool, bool, *argMismatch) {
	// The function is missing if its name is not found in the globals.
	if funv == nil {
		return true, false, nil
	}

	// The function is actually not a function if its name is not bound to a function.
	fun, ok := funv.(*starlark.Function)
	if !ok {
		return false, true, nil
	}

	// Get the function arguments.
	argc := fun.NumParams()
	var args []string
	for i := range argc {
		arg, _ := fun.Param(i)
		args = append(args, arg)
	}

	// The function is invalid if it does not have the right arguments.
	match := len(args) == len(requiredArgs)
	if match {
		sort.Strings(args)
		sort.Strings(requiredArgs)
		for i := range args {
			if args[i] != requiredArgs[i] {
				match = false
				break
			}
		}
	}

	if !match {
		return false, false, &argMismatch{gotten: args, expected: requiredArgs}
	}

	return false, false, nil
}

// Validate validates a scriptlet by compiling it and checking the presence of required and optional functions.
func Validate(compiler func(string, string) (*starlark.Program, error), programName string, src string, scriptletFunctions Declaration) error {
	// Try to compile the program.
	prog, err := compiler(programName, src)
	if err != nil {
		return err
	}

	thread := &starlark.Thread{Name: programName}
	globals, err := prog.Init(thread, nil)
	if err != nil {
		return err
	}

	globals.Freeze()

	var missingFuns []string
	mistypedFuns := make(map[scriptletFunction]string)
	mismatchingFuns := make(map[scriptletFunction]*argMismatch)
	errorsFound := false
	for fun, requiredArgs := range scriptletFunctions {
		funv := globals[fun.name]
		missing, mistyped, mismatch := validateFunction(funv, requiredArgs)

		if missing && !fun.optional || mistyped || mismatch != nil {
			errorsFound = true
			if missing {
				missingFuns = append(missingFuns, fun.name)
			} else if mistyped {
				mistypedFuns[fun] = funv.Type()
			} else {
				mismatchingFuns[fun] = mismatch
			}
		}
	}

	// Return early if everything looks good.
	if !errorsFound {
		return nil
	}

	errorText := ""
	sentences := 0

	// String builder to format pretty error messages.
	appendToError := func(text string) {
		var link string

		switch sentences {
		case 0:
			link = ""
		case 1:
			link = "; additionally, "
		default:
			link = "; finally, "
		}

		errorText += link
		errorText += text
		sentences++
	}

	switch len(missingFuns) {
	case 0:
	case 1:
		appendToError(fmt.Sprintf("the function %q is required but has not been found in the scriptlet", missingFuns[0]))
	default:
		appendToError(fmt.Sprintf("the functions %q are required but have not been found in the scriptlet", missingFuns))
	}

	if len(mistypedFuns) != 0 {
		var parts []string
		for fun, ty := range mistypedFuns {
			parts = append(parts, fmt.Sprintf("%q should define the scriptlet’s %s function of the same name (found a value of type %s instead)", fun.name, optionalToString(fun.optional), ty))
		}

		appendToError(strings.Join(parts, ", "))
	}

	if len(mismatchingFuns) != 0 {
		var parts []string
		for fun, args := range mismatchingFuns {
			parts = append(parts, fmt.Sprintf("the %s function %q defines arguments %q (expected %q)", optionalToString(fun.optional), fun.name, args.gotten, args.expected))
		}

		appendToError(strings.Join(parts, ", "))
	}

	return errors.New(errorText)
}

// Set compiles a scriptlet into memory. If empty src is provided the current program is deleted.
func (l *Loader) Set(compiler func(string, string) (*starlark.Program, error), programName string, src string) error {
	if src == "" {
		l.programsMu.Lock()
		delete(l.programs, programName)
		l.programsMu.Unlock()
	} else {
		prog, err := compiler(programName, src)
		if err != nil {
			return err
		}

		l.programsMu.Lock()
		l.programs[programName] = prog
		l.programsMu.Unlock()
	}

	return nil
}

// Program returns a precompiled scriptlet program.
func (l *Loader) Program(name string, programName string) (*starlark.Program, *starlark.Thread, error) {
	l.programsMu.Lock()
	prog, found := l.programs[programName]
	l.programsMu.Unlock()
	if !found {
		return nil, nil, fmt.Errorf("%s scriptlet not loaded", name)
	}

	thread := &starlark.Thread{Name: programName}

	return prog, thread, nil
}