fix: decode typed class instances into interface{} as Object#232
Open
JeroenSoeters wants to merge 1 commit intoapple:mainfrom
Open
fix: decode typed class instances into interface{} as Object#232JeroenSoeters wants to merge 1 commit intoapple:mainfrom
JeroenSoeters wants to merge 1 commit intoapple:mainfrom
Conversation
When the decoder encounters a typed class instance (e.g.
authBasic.Config#AuthorizedUser) and the Go target type is interface{},
fall back to decoding as a generic *Object instead of erroring with
'cannot decode Pkl value of type X into Go type interface{}'.
This happens when typed class instances appear as values inside
pkl.Object.Properties (which is map[string]any). The decoder already
handles Dynamic objects this way; typed classes with no registered Go
mapping should follow the same path.
If a Go struct mapping IS registered for the class, the existing typed
decoding path is still used.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the decoder encounters a typed class instance (e.g.,
MyModule#MyConfig) and the Go target type isinterface{}, it errors with:This happens because
decodeObjectonly routesDynamicobjects and explicitObject-typed targets todecodeObjectGeneric. All other typed classes are routed todecodeTyped, which requires a registered Go struct mapping.Dynamic objects and typed class instances have the same wire format - properties, entries, and elements. The only difference is the class name. There's no structural reason to reject typed classes when the Go target is
interface{}.Use case
We maintain a plugin architecture where plugin authors define typed PKL configuration classes that extend a base class:
The host application deserializes plugin config as
pkl.Object(via apkl.Object-tagged struct field) because it cannot (or shouldn't) know all plugin types at compile time. The config is passed through asjson.RawMessageto the plugin process, which handles its own deserialization.This breaks when the config contains typed class instances (like
AuthorizedUser) nested inside thepkl.Object.Propertiesmap - each property value targetsinterface{}, and typed classes are rejected.Registering mappings via
pkl.RegisterMappingis not viable here because plugin types are defined externally and are not known to the host at compile time.Fix
When the target type is
interface{}and no Go struct mapping is registered for the Pkl class, fall back todecodeObjectGeneric(producing a*pkl.Object). If a mapping IS registered, the existing typed decoding path is used.This is consistent with how
Dynamicobjects are already handled - typed classes are structurally identical on the wire.