Repository navigation
Use strings.Builder for querybuilder marshal method #5008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
96d6ab9
1ddc10c
9c7a604
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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 | ||
| 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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:
Combined they make sure that all *Object->ObjectID resolution happens in parallel
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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())) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.