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

Support for omitempty, as well as tests for omitempty. #81

Merged
merged 2 commits into from
May 1, 2015

Conversation

bbuck
Copy link
Contributor

@bbuck bbuck commented May 1, 2015

This pull request will close #73 and add in support for omitempty.

If you want me to clean things up a bit, I can - this works fine but would not be the best structure to add in future "options" to field names.

@bbuck
Copy link
Contributor Author

bbuck commented May 1, 2015

I've gone ahead and cleaned this up, turned into something a little more extendable.

I've also taken out the concept that zero numeric values are "empty" and thus would appear in omitempty fields when their values are 0. To compensate, I added omitzero which would omit numeric fields when their values are 0.

type Sample struct {
    User  string `toml:"name"`
    Pass  string `toml:"password,omitempty"`
    Count int    `toml:"count,omitzero"`
}

s := Sample{"username", "", 50}
// Encode =>
// name = "username"
// count = 50
// 

s.Pass = "some password"
s.Count = 0
// Encode =>
// name = "username"
// password = "some password"
//

BurntSushi added a commit that referenced this pull request May 1, 2015
Support for omitempty, as well as tests for omitempty.
@BurntSushi BurntSushi merged commit 056c9bc into BurntSushi:master May 1, 2015
@BurntSushi
Copy link
Owner

Looks great! Thank you!

@chowey chowey mentioned this pull request Aug 3, 2015
@cespare
Copy link
Collaborator

cespare commented Mar 3, 2016

It's unfortunate that omitzero is a separate concept from omitempty. This is an unnecessary break from encoding/json's behavior.

@cespare
Copy link
Collaborator

cespare commented Mar 3, 2016

See #120.

Luap99 added a commit to Luap99/common that referenced this pull request Oct 1, 2021
When toml writes the config file it does not use `omitempty` for numeric
values instead it requires `omitzero`. [1]

The problem is that without this change, `config.Write()` writes
```
[machine]
  cpus = 0
  disk_size = 0
  memory = 0
```
to the user file. Becuase podman machine system connection add code will
do this the config file is broken afterwads. The first vm will be created
successfully but after this every other vm will be broken because the
cpu, memory and disk size are set to zero.

[1] BurntSushi/toml#81

Fixes containers/podman#11824

Signed-off-by: Paul Holzinger <[email protected]>
Luap99 added a commit to Luap99/common that referenced this pull request Oct 1, 2021
When toml writes the config file it does not use `omitempty` for numeric
values instead it requires `omitzero`. [1]

The problem is that without this change, `config.Write()` writes
```
[machine]
  cpus = 0
  disk_size = 0
  memory = 0
```
to the user file. Because podman machine system connection add code will
do this the config file is broken afterwards. The first vm will be created
successfully but after this every other vm will be broken because the
cpu, memory and disk size are set to zero.

[1] BurntSushi/toml#81

Fixes containers/podman#11824

Signed-off-by: Paul Holzinger <[email protected]>
Luap99 added a commit to Luap99/common that referenced this pull request Oct 1, 2021
When toml writes the config file it does not use `omitempty` for numeric
values instead it requires `omitzero`. [1]

The problem is that without this change, `config.Write()` writes
```
[machine]
  cpus = 0
  disk_size = 0
  memory = 0
```
to the user file. Because podman machine system connection add code will
do this the config file is broken afterwards. The first vm will be created
successfully but after this every other vm will be broken because the
cpu, memory and disk size are set to zero.

[1] BurntSushi/toml#81

Fixes containers/podman#11824

Signed-off-by: Paul Holzinger <[email protected]>
Luap99 added a commit to Luap99/common that referenced this pull request Oct 1, 2021
When toml writes the config file it does not use `omitempty` for numeric
values instead it requires `omitzero`. [1]

The problem is that without this change, `config.Write()` writes
```
[machine]
  cpus = 0
  disk_size = 0
  memory = 0
```
to the user file. Because podman machine system connection add code will
do this the config file is broken afterwards. The first vm will be created
successfully but after this every other vm will be broken because the
cpu, memory and disk size are set to zero.

[1] BurntSushi/toml#81

Fixes containers/podman#11824

Signed-off-by: Paul Holzinger <[email protected]>
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.

Support "omitempty" like JSON and XML
3 participants