Skip to content

How to flatten the output? #1102

Answered by HT154
nocquidant asked this question in Q&A
Jun 19, 2025 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

Here's another (probably over-engineered) way to go about this using annotations and reflection:

// schema.pkl
// I've expanded the types with some additional test cases
import "pkl:reflect"

name: String

@Flatten
job: Job

class Job {
	title: String
	company: String
	exception: JobException
	@Flatten
	nested: JobNested
	@Flatten
	nestedMapping: Mapping<String, Any>
	@Flatten
	nestedDynamic: Dynamic
}

class JobException {
	foo: String
	foobar: String
	@Flatten
	nested: JobNested
}

class JobNested {
	bar: String
	baz: String
}

class Flatten extends Annotation {}

function doFlatten(obj: Any): Any =
	if (obj is Typed)
		let (objClazz = reflect.Class(obj.getClass()))
			getProps(objClazz…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@nocquidant
Comment options

@HT154
Comment options

HT154 Jun 20, 2025
Collaborator

@HT154
Comment options

HT154 Jun 20, 2025
Collaborator

Answer selected by nocquidant
@nocquidant
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants