/
platform.go
71 lines (55 loc) · 1.5 KB
/
platform.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
package core
import (
"encoding/json"
"fmt"
"github.com/containerd/containerd/platforms"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/vektah/gqlparser/v2/ast"
"github.com/dagger/dagger/dagql"
"github.com/dagger/dagger/dagql/call"
)
type Platform specs.Platform
func (p Platform) Spec() specs.Platform {
return specs.Platform(p)
}
func (p Platform) Format() string {
return platforms.Format(specs.Platform(p))
}
var _ dagql.Typed = Platform{}
func (p Platform) TypeName() string {
return "Platform"
}
func (p Platform) TypeDescription() string {
return dagql.FormatDescription(
`The platform config OS and architecture in a Container.`,
`The format is [os]/[platform]/[version] (e.g., "darwin/arm64/v7", "windows/amd64", "linux/arm64").`)
}
func (p Platform) Type() *ast.Type {
return &ast.Type{
NamedType: p.TypeName(),
NonNull: true,
}
}
var _ dagql.Input = Platform{}
func (p Platform) Decoder() dagql.InputDecoder {
return p
}
func (p Platform) ToLiteral() call.Literal {
return call.NewLiteralString(platforms.Format(specs.Platform(p)))
}
var _ dagql.ScalarType = Platform{}
func (Platform) DecodeInput(val any) (dagql.Input, error) {
switch x := val.(type) {
case string:
plat, err := platforms.Parse(x)
if err != nil {
return nil, err
}
return Platform(plat), nil
default:
return nil, fmt.Errorf("cannot convert %T to Platform", val)
}
}
func (p Platform) MarshalJSON() ([]byte, error) {
return json.Marshal(platforms.Format(specs.Platform(p)))
}