henry3260 commented on code in PR #77092:
URL: https://github.com/apache/airflow/pull/77092#discussion_r4727608423
##########
go-sdk/pkg/execution/client.go:
##########
@@ -521,95 +522,32 @@ func resolveExpiry(retention *time.Duration, now
time.Time) (*genmodels.Time, er
}
}
-// validateJSONRepresentable checks what the frame encoder actually emits, not
-// the Go value: a reflection walk has to mirror the encoder's field rules
(tags,
-// "-", omitempty, embedding, marshalers) and misjudges values wherever it
drifts.
-// On success it returns the encoded bytes, so the caller sends them instead of
-// encoding the value a second time, and whether the value encodes to null.
-func validateJSONRepresentable(value any) (msgpack.RawMessage, bool, error) {
- var buf bytes.Buffer
- if err := newFrameEncoder(&buf).Encode(value); err != nil {
- return nil, false, fmt.Errorf("%T is not JSON representable:
%w", value, err)
- }
- encoded := msgpack.RawMessage(buf.Bytes())
- // Decoded from its own reader so encoded keeps the whole payload.
- dec := msgpack.NewDecoder(bytes.NewReader(encoded))
- dec.SetMapDecoder(decodeStringKeyedMap)
- decoded, err := dec.DecodeInterface()
+// encodeJSONValue returns the msgpack form of value as encoding/json writes
it,
+// and whether that is null. The value must take encoding/json's shape, not
only
+// its field names: argument binding and UnmarshalJSONValue decode it with
+// encoding/json, and a ",string" field or an UnmarshalJSON method rejects a
+// value that was written any other way.
+func encodeJSONValue(value any) (encoded msgpack.RawMessage, isNull bool, err
error) {
+ blob, err := json.Marshal(value)
if err != nil {
return nil, false, fmt.Errorf("%T is not JSON representable:
%w", value, err)
}
- if err := validateDecodedValue(decoded); err != nil {
- return nil, false, err
- }
- return encoded, decoded == nil, nil
-}
-
-// decodeStringKeyedMap replaces msgpack's default map decoder, which panics on
-// a uuid.UUID key.
-func decodeStringKeyedMap(d *msgpack.Decoder) (any, error) {
- n, err := d.DecodeMapLen()
- if err != nil || n == -1 {
- return nil, err
- }
- m := make(map[string]any, n)
- for range n {
- k, err := d.DecodeInterface()
- if err != nil {
- return nil, err
- }
- key, ok := k.(string)
- if !ok {
- return nil, fmt.Errorf("map keys must be strings, got
%T", k)
- }
- if m[key], err = d.DecodeInterface(); err != nil {
- return nil, err
- }
+ dec := json.NewDecoder(bytes.NewReader(blob))
+ dec.UseNumber()
+ var decoded any
+ if err := dec.Decode(&decoded); err != nil {
+ return nil, false, fmt.Errorf("%T is not JSON representable:
%w", value, err)
}
- return m, nil
-}
-
-func validateDecodedValue(v any) error {
- switch v := v.(type) {
- case nil, string, bool,
- int, int8, int16, int32, int64,
- uint, uint8, uint16, uint32, uint64:
- return nil
- case float32:
- return checkFinite(float64(v))
- case float64:
- return checkFinite(v)
- case time.Time:
- return fmt.Errorf(
- "time.Time is not JSON representable; store
value.Format(time.RFC3339) " +
- "and parse it back with time.Parse",
- )
- case []byte:
- return fmt.Errorf(
- "[]byte is not JSON representable; encode it, for
example with base64.StdEncoding.EncodeToString",
- )
- case []any:
- for _, elem := range v {
- if err := validateDecodedValue(elem); err != nil {
- return err
- }
- }
- return nil
- case map[string]any:
- for _, elem := range v {
- if err := validateDecodedValue(elem); err != nil {
- return err
- }
- }
- return nil
- default:
- return fmt.Errorf("%T is not JSON representable", v)
+ if decoded, err = jsonvalue.ResolveNumbers(decoded, "value"); err !=
nil {
Review Comment:
I went with accepting it and calling it out, and here is why I did not take
the type-tree route.
A struct that has both a `time.Time` and a `float64` field contains a
`json.Marshaler`, so it would still take the JSON pass and its `5.0` would
still become `5`. The result for a float would then depend on what its sibling
fields are, which is harder to explain to a user than one rule that always
holds.
What I changed instead:
- `go.rst` now has a warning in the XCom type mapping section. It says a
downstream Python task receives `5` and not `5.0`, and that a Go task taking a
`float64` parameter is not affected.
- The PR description has its own section for this change.
---
Drafted-by: Claude Code (Fable 5.2); reviewed by @henry3260 before posting
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]