Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OrderedDict. Refactor Dict.render to utilize OrderedDict #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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