generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
model.go
75 lines (67 loc) · 1.32 KB
/
model.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
package model
import (
"errors"
"io"
"strconv"
"strings"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/sha256"
)
type Deployment struct {
Module string
Language string
Key DeploymentKey
Schema *schema.Module
Artefacts []*Artefact
}
// Close is a convenience function to close all artefacts.
func (d *Deployment) Close() error {
errs := make([]error, 0, len(d.Artefacts))
for _, a := range d.Artefacts {
errs = append(errs, a.Content.Close())
}
return errors.Join(errs...)
}
type Artefact struct {
Path string
Executable bool
Digest sha256.SHA256
// ~Zero-cost on-demand reader.
Content io.ReadCloser
}
type Labels map[string]any
func (l Labels) String() string {
w := strings.Builder{}
i := 0
for k, v := range l {
if i > 0 {
w.WriteString(" ")
}
i++
w.WriteString(k)
w.WriteString("=")
writeValue(&w, v)
}
return w.String()
}
func writeValue(w *strings.Builder, v any) {
switch v := v.(type) {
case string:
w.WriteString(v)
case float64:
w.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
case int:
w.WriteString(strconv.Itoa(v))
case bool:
w.WriteString(strconv.FormatBool(v))
case []any:
for i, v := range v {
if i > 0 {
w.WriteString(",")
}
writeValue(w, v)
}
default:
panic("unknown label type")
}
}