| 147 | } |
| 148 | |
| 149 | func (b *Builder) addJSONBody(ctx context.Context, jsonnetSnippet []byte, body interface{}) error { |
| 150 | buf := bytes.NewBuffer(make([]byte, 0, b.bodySizeHint)) |
| 151 | enc := json.NewEncoder(buf) |
| 152 | enc.SetEscapeHTML(false) |
| 153 | enc.SetIndent("", "") |
| 154 | |
| 155 | if err := enc.Encode(body); err != nil { |
| 156 | return errors.WithStack(err) |
| 157 | } |
| 158 | |
| 159 | vm, err := b.deps.JsonnetVM(ctx) |
| 160 | if err != nil { |
| 161 | return errors.WithStack(err) |
| 162 | } |
| 163 | vm.TLACode("ctx", buf.String()) |
| 164 | |
| 165 | res, err := vm.EvaluateAnonymousSnippet( |
| 166 | b.Config.TemplateURI, |
| 167 | string(jsonnetSnippet), |
| 168 | ) |
| 169 | if err != nil { |
| 170 | // Unfortunately we can not use errors.As / errors.Is, see: |
| 171 | // https://github.com/google/go-jsonnet/issues/592 |
| 172 | if strings.Contains(err.Error(), (&jsonnet.RuntimeError{Msg: "cancel"}).Error()) { |
| 173 | return errors.WithStack(ErrCancel) |
| 174 | } |
| 175 | |
| 176 | return errors.WithStack(err) |
| 177 | } |
| 178 | |
| 179 | rb := strings.NewReader(res) |
| 180 | if err := b.r.SetBody(io.NopCloser(rb)); err != nil { |
| 181 | return errors.WithStack(err) |
| 182 | } |
| 183 | |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | func (b *Builder) addURLEncodedBody(ctx context.Context, jsonnetSnippet []byte, body interface{}) error { |
| 188 | buf := bytes.NewBuffer(make([]byte, 0, b.bodySizeHint)) |