Skip to content

Conversation

@rainforest-bailey
Copy link

I would like to add a companion type to 'Dict', an 'OrderedDict', that will retain original ordering on keys.

@rainforest-bailey
Copy link
Author

For additional context on why this feature is useful. When generating code to initialize a struct, it is beneficial to be able to control the order in which fields are defined. I found myself comparing my code gen to an existing spec definition. Ideally, I would be able to visually scan the 2 side by side, but I had to search each field individually, which was more cumbersome.

@dave
Copy link
Owner

dave commented Dec 1, 2024

When creating a struct definition, you don't need to use Dict... the Struct method preserves the order of items:

c := Type().Id("foo").Struct(
	List(Id("x"), Id("y")).Int(),
	Id("u").Float32(),
)
fmt.Printf("%#v", c)
// Output:
// type foo struct {
// 	x, y int
// 	u    float32
// }

See https://github.com/dave/jennifer?tab=readme-ov-file#interface-struct

@rainforest-bailey
Copy link
Author

@dave How would I initialize a struct with a defined field ordering?

@dave
Copy link
Owner

dave commented Dec 5, 2024

Can you show me the code you want it to output?

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