Skip to content

Allow developers to implement and process their own json.Options types #17

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

Closed
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
9 changes: 9 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package json

import (
"github.com/go-json-experiment/json/internal/jsonflags"
)

type (
BoolFlags = jsonflags.Bools
)
4 changes: 1 addition & 3 deletions internal/jsonflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// These flags are shared across both "json", "jsontext", and "jsonopts".
package jsonflags

import "github.com/go-json-experiment/json/internal"

// Bools represents zero or more boolean flags, all set to true or false.
// The least-significant bit is the boolean value of all flags in the set.
// The remaining bits identify which particular flags.
Expand All @@ -17,7 +15,7 @@ import "github.com/go-json-experiment/json/internal"
// - (Expand | Indent | 1) means "Expand and Indent are true"
type Bools uint64

func (Bools) JSONOptions(internal.NotForPublicUse) {}
func (Bools) JSONOptions() {}

const (
// AllFlags is the set of all flags.
Expand Down
21 changes: 13 additions & 8 deletions internal/jsonopts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
package jsonopts

import (
"github.com/go-json-experiment/json/internal"
"github.com/go-json-experiment/json/internal/jsonflags"
)

// Options is the common options type shared across json packages.
type Options interface {
// JSONOptions is exported so related json packages can implement Options.
JSONOptions(internal.NotForPublicUse)
JSONOptions()
}

// Struct is the combination of all options in struct form.
Expand Down Expand Up @@ -72,7 +71,7 @@ func (dst *Struct) CopyCoderOptions(src *Struct) {
dst.CoderValues = src.CoderValues
}

func (*Struct) JSONOptions(internal.NotForPublicUse) {}
func (*Struct) JSONOptions() {}

// GetUnknownOption is injected by the "json" package to handle Options
// declared in that package so that "jsonopts" can handle them.
Expand Down Expand Up @@ -170,7 +169,13 @@ func (dst *Struct) Join(srcs ...Options) {
dst.FormatDepth = src.FormatDepth
}
default:
JoinUnknownOption(dst, src)
unknown := true
if joiner, ok := src.(interface{ Join(*Struct, Options) bool }); ok {
unknown = joiner.Join(dst, src)
}
if unknown {
JoinUnknownOption(dst, src)
}
Comment on lines +172 to +178
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the core logic of the PR. Everything else is making private symbols public that are needed to use this logic.

}
}
}
Expand All @@ -184,7 +189,7 @@ type (
// type for jsonflags.Unmarshalers declared in "json" package
)

func (Indent) JSONOptions(internal.NotForPublicUse) {}
func (IndentPrefix) JSONOptions(internal.NotForPublicUse) {}
func (ByteLimit) JSONOptions(internal.NotForPublicUse) {}
func (DepthLimit) JSONOptions(internal.NotForPublicUse) {}
func (Indent) JSONOptions() {}
func (IndentPrefix) JSONOptions() {}
func (ByteLimit) JSONOptions() {}
func (DepthLimit) JSONOptions() {}
6 changes: 3 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package json
import (
"fmt"

"github.com/go-json-experiment/json/internal"
"github.com/go-json-experiment/json/internal/jsonflags"
"github.com/go-json-experiment/json/internal/jsonopts"
)
Expand Down Expand Up @@ -69,6 +68,7 @@ import (
//
// Options that do not affect a particular operation are ignored.
type Options = jsonopts.Options
type OptionsStruct = jsonopts.Struct

// JoinOptions coalesces the provided list of options into a single Options.
// Properties set in later options override the value of previously set properties.
Expand Down Expand Up @@ -237,8 +237,8 @@ type (
unmarshalersOption Unmarshalers
)

func (*marshalersOption) JSONOptions(internal.NotForPublicUse) {}
func (*unmarshalersOption) JSONOptions(internal.NotForPublicUse) {}
func (*marshalersOption) JSONOptions() {}
func (*unmarshalersOption) JSONOptions() {}

// Inject support into "jsonopts" to handle these types.
func init() {
Expand Down