-
Notifications
You must be signed in to change notification settings - Fork 9
/
env.go
61 lines (48 loc) · 1.43 KB
/
env.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package args
import (
"context"
"encoding/json"
"strings"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
type EnvMap struct {
Values map[string]ResolvableValue
}
var _ json.Unmarshaler = &EnvMap{}
func (cem *EnvMap) UnmarshalJSON(data []byte) error {
var values map[string]ResolvableValue
if err := json.Unmarshal(data, &values); err != nil {
return err
}
cem.Values = values
return nil
}
func (cem *EnvMap) Parsed(ctx context.Context, pl pkggraph.PackageLoader, loc pkggraph.Location) ([]*schema.BinaryConfig_EnvEntry, error) {
if cem == nil {
return nil, nil
}
var env []*schema.BinaryConfig_EnvEntry
for key, value := range cem.Values {
out, err := value.ToProto(ctx, pl, loc)
if err != nil {
return nil, err
}
env = append(env, &schema.BinaryConfig_EnvEntry{Name: key, Value: out})
}
slices.SortFunc(env, func(a, b *schema.BinaryConfig_EnvEntry) bool {
return strings.Compare(a.Name, b.Name) < 0
})
return env, nil
}
func mustString(what string, value any) (string, error) {
if str, ok := value.(string); ok {
return str, nil
}
return "", fnerrors.BadInputError("%s: expected a string", what)
}