forked from dagger/agents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdagger.gen.go
258 lines (233 loc) · 7.49 KB
/
dagger.gen.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
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
247
248
249
250
251
252
253
254
255
256
257
258
// Code generated by dagger. DO NOT EDIT.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"github.com/vektah/gqlparser/v2/gqlerror"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"go.opentelemetry.io/otel/trace"
"dagger/toy-workspace/internal/dagger"
"dagger/toy-workspace/internal/querybuilder"
"dagger/toy-workspace/internal/telemetry"
)
var dag = dagger.Connect()
func Tracer() trace.Tracer {
return otel.Tracer("dagger.io/sdk.go")
}
// used for local MarshalJSON implementations
var marshalCtx = context.Background()
// called by main()
func setMarshalContext(ctx context.Context) {
marshalCtx = ctx
dagger.SetMarshalContext(ctx)
}
type DaggerObject = querybuilder.GraphQLMarshaller
type ExecError = dagger.ExecError
// ptr returns a pointer to the given value.
func ptr[T any](v T) *T {
return &v
}
// convertSlice converts a slice of one type to a slice of another type using a
// converter function
func convertSlice[I any, O any](in []I, f func(I) O) []O {
out := make([]O, len(in))
for i, v := range in {
out[i] = f(v)
}
return out
}
func (r ToyWorkspace) MarshalJSON() ([]byte, error) {
var concrete struct {
Container *dagger.Container
}
concrete.Container = r.Container
return json.Marshal(&concrete)
}
func (r *ToyWorkspace) UnmarshalJSON(bs []byte) error {
var concrete struct {
Container *dagger.Container
}
err := json.Unmarshal(bs, &concrete)
if err != nil {
return err
}
r.Container = concrete.Container
return nil
}
func main() {
ctx := context.Background()
// Direct slog to the new stderr. This is only for dev time debugging, and
// runtime errors/warnings.
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelWarn,
})))
if err := dispatch(ctx); err != nil {
os.Exit(2)
}
}
func unwrapError(rerr error) string {
var gqlErr *gqlerror.Error
if errors.As(rerr, &gqlErr) {
return gqlErr.Message
}
return rerr.Error()
}
func dispatch(ctx context.Context) (rerr error) {
ctx = telemetry.InitEmbedded(ctx, resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("dagger-go-sdk"),
// TODO version?
))
defer telemetry.Close()
// A lot of the "work" actually happens when we're marshalling the return
// value, which entails getting object IDs, which happens in MarshalJSON,
// which has no ctx argument, so we use this lovely global variable.
setMarshalContext(ctx)
fnCall := dag.CurrentFunctionCall()
defer func() {
if rerr != nil {
if err := fnCall.ReturnError(ctx, dag.Error(unwrapError(rerr))); err != nil {
fmt.Println("failed to return error:", err)
}
}
}()
parentName, err := fnCall.ParentName(ctx)
if err != nil {
return fmt.Errorf("get parent name: %w", err)
}
fnName, err := fnCall.Name(ctx)
if err != nil {
return fmt.Errorf("get fn name: %w", err)
}
parentJson, err := fnCall.Parent(ctx)
if err != nil {
return fmt.Errorf("get fn parent: %w", err)
}
fnArgs, err := fnCall.InputArgs(ctx)
if err != nil {
return fmt.Errorf("get fn args: %w", err)
}
inputArgs := map[string][]byte{}
for _, fnArg := range fnArgs {
argName, err := fnArg.Name(ctx)
if err != nil {
return fmt.Errorf("get fn arg name: %w", err)
}
argValue, err := fnArg.Value(ctx)
if err != nil {
return fmt.Errorf("get fn arg value: %w", err)
}
inputArgs[argName] = []byte(argValue)
}
result, err := invoke(ctx, []byte(parentJson), parentName, fnName, inputArgs)
if err != nil {
var exec *dagger.ExecError
if errors.As(err, &exec) {
return exec.Unwrap()
}
return err
}
resultBytes, err := json.Marshal(result)
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
if err := fnCall.ReturnValue(ctx, dagger.JSON(resultBytes)); err != nil {
return fmt.Errorf("store return value: %w", err)
}
return nil
}
func invoke(ctx context.Context, parentJSON []byte, parentName string, fnName string, inputArgs map[string][]byte) (_ any, err error) {
_ = inputArgs
switch parentName {
case "ToyWorkspace":
switch fnName {
case "Read":
var parent ToyWorkspace
err = json.Unmarshal(parentJSON, &parent)
if err != nil {
panic(fmt.Errorf("%s: %w", "failed to unmarshal parent object", err))
}
var path string
if inputArgs["path"] != nil {
err = json.Unmarshal([]byte(inputArgs["path"]), &path)
if err != nil {
panic(fmt.Errorf("%s: %w", "failed to unmarshal input arg path", err))
}
}
return (*ToyWorkspace).Read(&parent, ctx, path)
case "Write":
var parent ToyWorkspace
err = json.Unmarshal(parentJSON, &parent)
if err != nil {
panic(fmt.Errorf("%s: %w", "failed to unmarshal parent object", err))
}
var path string
if inputArgs["path"] != nil {
err = json.Unmarshal([]byte(inputArgs["path"]), &path)
if err != nil {
panic(fmt.Errorf("%s: %w", "failed to unmarshal input arg path", err))
}
}
var content string
if inputArgs["content"] != nil {
err = json.Unmarshal([]byte(inputArgs["content"]), &content)
if err != nil {
panic(fmt.Errorf("%s: %w", "failed to unmarshal input arg content", err))
}
}
return (*ToyWorkspace).Write(&parent, path, content), nil
case "Build":
var parent ToyWorkspace
err = json.Unmarshal(parentJSON, &parent)
if err != nil {
panic(fmt.Errorf("%s: %w", "failed to unmarshal parent object", err))
}
return (*ToyWorkspace).Build(&parent, ctx)
case "":
var parent ToyWorkspace
err = json.Unmarshal(parentJSON, &parent)
if err != nil {
panic(fmt.Errorf("%s: %w", "failed to unmarshal parent object", err))
}
return New(), nil
default:
return nil, fmt.Errorf("unknown function %s", fnName)
}
case "":
return dag.Module().
WithDescription("A toy workspace for editing and building go programs\n").
WithObject(
dag.TypeDef().WithObject("ToyWorkspace", dagger.TypeDefWithObjectOpts{SourceMap: dag.SourceMap("main.go", 18, 6)}).
WithFunction(
dag.Function("Read",
dag.TypeDef().WithKind(dagger.TypeDefKindStringKind)).
WithDescription("Read a file").
WithSourceMap(dag.SourceMap("main.go", 25, 1)).
WithArg("path", dag.TypeDef().WithKind(dagger.TypeDefKindStringKind), dagger.FunctionWithArgOpts{Description: "The path of the file", SourceMap: dag.SourceMap("main.go", 28, 2)})).
WithFunction(
dag.Function("Write",
dag.TypeDef().WithObject("ToyWorkspace")).
WithDescription("Write a file").
WithSourceMap(dag.SourceMap("main.go", 33, 1)).
WithArg("path", dag.TypeDef().WithKind(dagger.TypeDefKindStringKind), dagger.FunctionWithArgOpts{Description: "The path of the file", SourceMap: dag.SourceMap("main.go", 35, 2)}).
WithArg("content", dag.TypeDef().WithKind(dagger.TypeDefKindStringKind), dagger.FunctionWithArgOpts{Description: "The content to write", SourceMap: dag.SourceMap("main.go", 37, 2)})).
WithFunction(
dag.Function("Build",
dag.TypeDef().WithKind(dagger.TypeDefKindStringKind)).
WithDescription("Build the code at the current directory in the workspace").
WithSourceMap(dag.SourceMap("main.go", 44, 1))).
WithField("Container", dag.TypeDef().WithObject("Container"), dagger.TypeDefWithFieldOpts{Description: "The workspace container.", SourceMap: dag.SourceMap("main.go", 21, 2)}).
WithConstructor(
dag.Function("New",
dag.TypeDef().WithObject("ToyWorkspace")).
WithSourceMap(dag.SourceMap("main.go", 9, 1)))), nil
default:
return nil, fmt.Errorf("unknown object %s", parentName)
}
}