Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 37 additions & 42 deletions sdk/go/internal/querybuilder/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"context"
"fmt"
"reflect"
"strconv"
"strings"

"golang.org/x/sync/errgroup"
)

// GraphQLMarshaller is an internal interface for marshalling an object into GraphQL.
Expand Down Expand Up @@ -55,67 +54,63 @@ func marshalValue(ctx context.Context, v reflect.Value) (string, error) {

switch t.Kind() {
case reflect.Bool:
return fmt.Sprintf("%t", v.Bool()), nil
return strconv.FormatBool(v.Bool()), nil
case reflect.Int:
return fmt.Sprintf("%d", v.Int()), nil
return strconv.FormatInt(v.Int(), 10), nil
case reflect.String:
name := t.Name()
// distinguish enum const values and customScalars from string type
// GraphQL complains if you try to put a string literal in place of an enum: FOO vs "FOO"
_, found := customScalar[t.Name()]
if name != "string" && !found {
return fmt.Sprintf("%s", v.String()), nil //nolint:gosimple,staticcheck
return v.String(), nil
}
return fmt.Sprintf("%q", v.String()), nil //nolint:gosimple,staticcheck
return strconv.Quote(v.String()), nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I didn't realize we were just quoting Go strings to pass them to GraphQL - it seems like that could fail someday if they have different escape syntax, but I haven't found a way to break it yet, so 🤷‍♂️ guess it's fine

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fears have come true: #5069

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right -- those should be escaped. Impressed this worked for so long.

case reflect.Pointer:
if v.IsNil() {
return "null", nil
}
return marshalValue(ctx, v.Elem())
case reflect.Slice:
var b strings.Builder
b.WriteRune('[')
n := v.Len()
elems := make([]string, n)
eg, gctx := errgroup.WithContext(ctx)
for i := 0; i < n; i++ {
i := i
eg.Go(func() error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that we did these in parallel before, perhaps a micro-optimization but I wonder if there were cases where it helped.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, totally missed this comment.

It's because of "pass by object".

When passing a *Container type, there's a custom marshaller doing the *Container --> ContainerID conversion which issues an API call: https://github.com/dagger/dagger/blob/main/sdk/go/api.gen.go#L79

The problem is worsened since this is a recursive: passing an array with 10 elements, each containing a struct with 10 fields will result in 100 sequential API calls. On the upside, I don't think the API currently has a scenario where that is possible.

There are two layers of parallelism:

  • querybuilder.go:marshalArguments(): For a given GraphQL call, process all arguments in parallel (e.g. if chaining 10 things -- take all passed arguments and resolve them at once)
  • this code: for each argument, resolve each slice and struct elements in parallel

Combined they make sure that all *Object->ObjectID resolution happens in parallel

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aluzzardi Ah - I didn't appreciate that marshaling involves sending an API request, that makes sense. Sounds important; will revert and reopen.

m, err := marshalValue(gctx, v.Index(i))
if err != nil {
return err
}
elems[i] = m
return nil
})
}
if err := eg.Wait(); err != nil {
return "", err
m, err := marshalValue(ctx, v.Index(i))
if err != nil {
return "", err
}
if i > 0 {
b.WriteRune(',')
}
b.WriteString(m)
}
return fmt.Sprintf("[%s]", strings.Join(elems, ",")), nil
b.WriteRune(']')
return b.String(), nil
case reflect.Struct:
var b strings.Builder
b.WriteRune('{')
n := v.NumField()
elems := make([]string, n)
eg, gctx := errgroup.WithContext(ctx)
for i := 0; i < n; i++ {
i := i
eg.Go(func() error {
f := t.Field(i)
name := f.Name
tag := strings.SplitN(f.Tag.Get("json"), ",", 2)[0]
if tag != "" {
name = tag
}
m, err := marshalValue(gctx, v.Field(i))
if err != nil {
return err
}
elems[i] = fmt.Sprintf("%s:%s", name, m)
return nil
})
}
if err := eg.Wait(); err != nil {
return "", err
f := t.Field(i)
m, err := marshalValue(ctx, v.Field(i))
if err != nil {
return "", err
}
if i > 0 {
b.WriteRune(',')
}
tag := strings.SplitN(f.Tag.Get("json"), ",", 2)[0]
if tag == "" {
b.WriteString(f.Name)
} else {
b.WriteString(tag)
}
b.WriteRune(':')
b.WriteString(m)
}
return fmt.Sprintf("{%s}", strings.Join(elems, ",")), nil
b.WriteRune('}')
return b.String(), nil
default:
panic(fmt.Errorf("unsupported argument of kind %s", t.Kind()))
}
Expand Down