Skip to content

Add option to encode value keys with '.' separators#229

Open
gKits wants to merge 5 commits into
gorilla:mainfrom
gKits:main
Open

Add option to encode value keys with '.' separators#229
gKits wants to merge 5 commits into
gorilla:mainfrom
gKits:main

Conversation

@gKits

@gKits gKits commented Nov 28, 2024

Copy link
Copy Markdown

Description

The Encoder struct has now a method called ActivateKeySeparation.
When this option is activated the keys of the values map[string][]string
will be the full path containing all names of the nested struct fields separated
by a period (.).

This also fixes the bug referenced in #228 that it is currently not possible to
decode values into structs with nested fields after encoding said structs.

I'm not sure about the naming so I'm open for suggestions if someone has better ideas.

Example

f := Foo{
    X: "hello",
    Y: Bar{
        A: "world",
        B: true,
    },
}

vals := map[string][]string{}
encoder := schema.NewEncoder()
encoder.ActivateKeySeparation()

encoder.Encode(f, vals)
fmt.Println(vals) // output: map[y.a:[test] y.b:[true] x:[test]]

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update
  • Go Version Update
  • Dependency Update

Description

Related Tickets & Documents

Added/updated tests?

  • Yes
  • No, and this is why: please replace this line with details on why tests
    have not been included
  • I need help with writing tests

Run verifications and test

  • make verify is passing
  • make test is passing

An Encoder can has the option to separate the names of nested
structs with a period when it constructs the values key during
encoding.
@gKits

gKits commented Nov 28, 2024

Copy link
Copy Markdown
Author

Make verify seems to fail due to some linter problems due to integer overflow conversions

@gKits gKits marked this pull request as ready for review November 28, 2024 12:32
@zak905

zak905 commented Nov 28, 2024

Copy link
Copy Markdown
Contributor

Looks good overall. If I may be a bit picky, I believe we could find a name which is semantically better for the option. I am thinking about something like: keepKeyOriginalPath or just keyOrignalPath

Also, I believe the Activate word is not necessary at the beginning of the setter function name. To keep in line with other options you can use the name directly with a boolean argument. the ZeroEmpty option is an example: https://github.com/gorilla/schema/blob/main/decoder.go#L46

@gKits

gKits commented Nov 29, 2024

Copy link
Copy Markdown
Author

@zak905 Thanks for the feedback. I have changed the name of the option to KeyOriginalPath definitely sounds better than before.

@gKits gKits requested a review from zak905 November 29, 2024 06:52

@zak905 zak905 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.

LGTM, thanks for addressing the remark.

Waiting on @jaitaiwan feedback.

@gKits

gKits commented Jun 18, 2025

Copy link
Copy Markdown
Author

Will someone look into this. I feel like having a consistent and working Encode->Decode->Encode behaviour is quite important. Are there any problems with the PR?

@zak905

zak905 commented Jun 18, 2025

Copy link
Copy Markdown
Contributor

Hi @gKits, I am gonna contact @jaitaiwan, let's see if we can get this merged.

@jaitaiwan

Copy link
Copy Markdown

I haven't had luck getting views on this I'm sorry. I'm no longer a maintainer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Encoding of nested struct is missing named prefix

4 participants