Skip to content

Decode Pkl Bytes into interface values - #277

Merged
HT154 merged 1 commit into
apple:mainfrom
protonnegativo:fix/decode-bytes-any
Oct 27, 2026
Merged

HT154 merged 1 commit into
apple:mainfrom
protonnegativo:fix/decode-bytes-any

Conversation

@protonnegativo

Copy link
Copy Markdown
Contributor

Decoding a Pkl Bytes value into an interface fails:

an internal error occurred: encountered unknown object code: 0x0f

decodeSlice handles codeBytes when the target is []byte, but decodePklObject (used for any targets) has no case for it. So a Bytes value fails to decode wherever the Go type is any. That includes a property typed Any, elements of a Listing<Any>, the members of a Dynamic, and EvaluateExpression into var out any.

To reproduce with real Pkl output, decode the committed test_fixtures/msgpack/collections.pkl.msgpack (res14: Bytes = Bytes(1, 2, 3, 4, 255)) into any with no registered schema:

b, _ := os.ReadFile("test_fixtures/msgpack/collections.pkl.msgpack")
v, err := newDecoder(b, map[string]reflect.Type{}).Decode(reflect.TypeFor[any]())
// before: err = encountered unknown object code: 0x0f
// after:  v.Interface().(Object).Properties["res14"] == []byte{0x1, 0x2, 0x3, 0x4, 0xff}

This change decodes it to []byte, the same type used for typed Bytes properties.

Testing: added a TestDecoder_Decode case ("should successfully decode interface with Pkl Bytes"). It fails on main and passes with the fix. go vet ./pkl/ is clean. In go test ./pkl/, the only failures are the tests that run the pkl CLI, which I don't have installed locally, and the same tests fail on main.

Note: when the decoded value is any, a Bytes used as a Set element or Map key still can't be decoded, because []byte is not a valid Go map key. With this change that case panics in reflect.SetMapIndex instead of returning the error above. This is the same as what already happens on main for Set { List() } into any, so I left it out of scope.

Decoding a Bytes value into `any` (for example a property typed `Any`,
or an element of a Dynamic or Listing<Any>) failed with "encountered
unknown object code: 0x0f", because decodePklObject only handled
Bytes when the target was a []byte. Decode it to []byte, the same Go
type used for typed Bytes properties.

@HT154 HT154 left a comment

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.

Thanks for the fix!

@HT154
HT154 merged commit 4418a76 into apple:main Oct 27, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants