Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ In Go, variables declared without an explicit initial value are given their zero

## Marshalling/Unmarshalling JSON

**Note:** v0.6.0 introduces a potential breaking change to anyone depending on marshalling non-present values to their zero values instead of null. See: [#9](https://github.com/markphelps/optional/pull/9) for more context.

Option types also marshal to/from JSON as you would expect:

### Marshalling
Expand Down Expand Up @@ -165,3 +167,7 @@ See [example_test.go](example_test.go) and the [documentation](http://godoc.org/
1. Commit your changes (`git commit -am 'Add some feature'`)
1. Push to the branch (`git push origin my-new-feature`)
1. Create a new Pull Request

### Golden Files

If changing the API you may need to update the [golden files](https://medium.com/soon-london/testing-with-golden-files-in-go-7fccc71c43d3) for your tests to pass by running `go test ./cmd/optional/... -update`.
11 changes: 8 additions & 3 deletions bool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions byte.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions cmd/optional/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,16 @@ func ({{ .VariableName }} {{ .OutputName }}) MarshalJSON() ([]byte, error) {
if {{ .VariableName }}.Present() {
return json.Marshal({{ .VariableName }}.value)
}
var zero {{ .TypeName }}
return json.Marshal(zero)
return json.Marshal(nil)
}

func ({{ .VariableName }} *{{ .OutputName }}) UnmarshalJSON(data []byte) error {

if string(data) == "null" {
{{ .VariableName }}.value = nil
return nil
}

var value {{ .TypeName }}

if err := json.Unmarshal(data, &value); err != nil {
Expand Down
11 changes: 8 additions & 3 deletions cmd/optional/testdata/optional_bar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions cmd/optional/testdata/optional_foo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions cmd/optional/testdata/string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions complex128.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions complex64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions error.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 25 additions & 9 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func Example_set() {

func Example_marshalJSON() {
type example struct {
Field *optional.String `json:"field,omitempty"`
Field *optional.String `json:"field,omitempty"`
FieldTwo *optional.String `json:"field_two"`
}

var values = []optional.String{
Expand All @@ -110,28 +111,35 @@ func Example_marshalJSON() {

for _, v := range values {
out, _ := json.Marshal(&example{
Field: &v,
Field: &v,
FieldTwo: &v,
})
fmt.Println(string(out))
}

out, _ := json.Marshal(&example{})
fmt.Println(string(out))

// Output:
// {"field":"foo"}
// {"field":""}
// {"field":"bar"}
// {"field":"foo","field_two":"foo"}
// {"field":"","field_two":""}
// {"field":"bar","field_two":"bar"}
// {"field_two":null}
}

func Example_unmarshalJSON() {
var values = []string{
`{"field":"foo"}`,
`{"field":""}`,
`{"field":"bar"}`,
`{"field":"foo","field_two":"foo"}`,
`{"field":"","field_two":""}`,
`{"field":"null","field_two":"null"}`,
`{"field":"bar","field_two":"bar"}`,
"{}",
}

for _, v := range values {
var o = &struct {
Field optional.String `json:"field,omitempty"`
Field optional.String `json:"field,omitempty"`
FieldTwo optional.String `json:"field_two"`
}{}

if err := json.Unmarshal([]byte(v), o); err != nil {
Expand All @@ -141,10 +149,18 @@ func Example_unmarshalJSON() {
o.Field.If(func(s string) {
fmt.Println(s)
})

o.FieldTwo.If(func(s string) {
fmt.Println(s)
})
}

// Output:
// foo
// foo
//

//
// bar
// bar
}
11 changes: 8 additions & 3 deletions float32.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions float64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions int.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading