Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
f5de14a6
Unverified
Commit
f5de14a6
authored
Apr 17, 2019
by
Kubernetes Prow Robot
Committed by
GitHub
Apr 17, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #76679 from stealthybox/serializer-options-followup
Make Serializer.options private and immutable + improve godoc
parents
944f76bf
4a141529
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
26 deletions
+32
-26
json.go
...c/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go
+30
-24
json_test.go
....io/apimachinery/pkg/runtime/serializer/json/json_test.go
+2
-2
No files found.
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go
View file @
f5de14a6
...
@@ -37,7 +37,7 @@ import (
...
@@ -37,7 +37,7 @@ import (
// is not nil, the object has the group, version, and kind fields set.
// is not nil, the object has the group, version, and kind fields set.
// Deprecated: use NewSerializerWithOptions instead.
// Deprecated: use NewSerializerWithOptions instead.
func
NewSerializer
(
meta
MetaFactory
,
creater
runtime
.
ObjectCreater
,
typer
runtime
.
ObjectTyper
,
pretty
bool
)
*
Serializer
{
func
NewSerializer
(
meta
MetaFactory
,
creater
runtime
.
ObjectCreater
,
typer
runtime
.
ObjectTyper
,
pretty
bool
)
*
Serializer
{
return
NewSerializerWithOptions
(
meta
,
creater
,
typer
,
&
SerializerOptions
{
false
,
pretty
,
false
})
return
NewSerializerWithOptions
(
meta
,
creater
,
typer
,
SerializerOptions
{
false
,
pretty
,
false
})
}
}
// NewYAMLSerializer creates a YAML serializer that handles encoding versioned objects into the proper YAML form. If typer
// NewYAMLSerializer creates a YAML serializer that handles encoding versioned objects into the proper YAML form. If typer
...
@@ -45,40 +45,46 @@ func NewSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtim
...
@@ -45,40 +45,46 @@ func NewSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtim
// matches JSON, and will error if constructs are used that do not serialize to JSON.
// matches JSON, and will error if constructs are used that do not serialize to JSON.
// Deprecated: use NewSerializerWithOptions instead.
// Deprecated: use NewSerializerWithOptions instead.
func
NewYAMLSerializer
(
meta
MetaFactory
,
creater
runtime
.
ObjectCreater
,
typer
runtime
.
ObjectTyper
)
*
Serializer
{
func
NewYAMLSerializer
(
meta
MetaFactory
,
creater
runtime
.
ObjectCreater
,
typer
runtime
.
ObjectTyper
)
*
Serializer
{
return
NewSerializerWithOptions
(
meta
,
creater
,
typer
,
&
SerializerOptions
{
true
,
false
,
false
})
return
NewSerializerWithOptions
(
meta
,
creater
,
typer
,
SerializerOptions
{
true
,
false
,
false
})
}
}
// NewSerializerWithOptions creates a JSON/YAML serializer that handles encoding versioned objects into the proper JSON/YAML
// NewSerializerWithOptions creates a JSON/YAML serializer that handles encoding versioned objects into the proper JSON/YAML
// form. If typer is not nil, the object has the group, version, and kind fields set.
// form. If typer is not nil, the object has the group, version, and kind fields set. Options are copied into the Serializer
func
NewSerializerWithOptions
(
meta
MetaFactory
,
creater
runtime
.
ObjectCreater
,
typer
runtime
.
ObjectTyper
,
serializerOptions
*
SerializerOptions
)
*
Serializer
{
// and are immutable.
func
NewSerializerWithOptions
(
meta
MetaFactory
,
creater
runtime
.
ObjectCreater
,
typer
runtime
.
ObjectTyper
,
options
SerializerOptions
)
*
Serializer
{
return
&
Serializer
{
return
&
Serializer
{
meta
:
meta
,
meta
:
meta
,
creater
:
creater
,
creater
:
creater
,
typer
:
typer
,
typer
:
typer
,
SerializerOptions
:
serializerO
ptions
,
options
:
o
ptions
,
}
}
}
}
// SerializerOptions holds the options which are used to creating a JSON/YAML serializer.
// SerializerOptions holds the options which are used to configure a JSON/YAML serializer.
// For example:
// example:
// (1) we can creates a JSON serializer once we set `Yaml` to `false`.
// (1) To configure a JSON serializer, set `Yaml` to `false`.
// (2) we can creates a YAML serializer once we set `Yaml` to `true`. This serializer supports only the subset of YAML that
// (2) To configure a YAML serializer, set `Yaml` to `true`.
// matches JSON, and will error if constructs are used that do not serialize to JSON.
// (3) To configure a strict serializer that can return strictDecodingError, set `Strict` to `true`.
// Please note that `Pretty` is silently ignored when `Yaml` is `true`.
// (3) we can creates a strict JSON/YAML serializer that can also return errors of type strictDecodingError, once we set
// `Strict` to `true`. And note that this serializer is not as performant as the non-strict variant, and should not be
// used in fast paths.
type
SerializerOptions
struct
{
type
SerializerOptions
struct
{
Yaml
bool
// Yaml: configures the Serializer to work with JSON(false) or YAML(true).
// When `Yaml` is enabled, this serializer only supports the subset of YAML that
// matches JSON, and will error if constructs are used that do not serialize to JSON.
Yaml
bool
// Pretty: configures a JSON enabled Serializer(`Yaml: false`) to produce human-readable output.
// This option is silently ignored when `Yaml` is `true`.
Pretty
bool
Pretty
bool
// Strict: configures the Serializer to return strictDecodingError's when duplicate fields are present decoding JSON or YAML.
// Note that enabling this option is not as performant as the non-strict variant, and should not be used in fast paths.
Strict
bool
Strict
bool
}
}
type
Serializer
struct
{
type
Serializer
struct
{
meta
MetaFactory
meta
MetaFactory
options
SerializerOptions
creater
runtime
.
ObjectCreater
creater
runtime
.
ObjectCreater
typer
runtime
.
ObjectTyper
typer
runtime
.
ObjectTyper
*
SerializerOptions
}
}
// Serializer implements Serializer
// Serializer implements Serializer
...
@@ -193,7 +199,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
...
@@ -193,7 +199,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
}
}
data
:=
originalData
data
:=
originalData
if
s
.
Yaml
{
if
s
.
options
.
Yaml
{
altered
,
err
:=
yaml
.
YAMLToJSON
(
data
)
altered
,
err
:=
yaml
.
YAMLToJSON
(
data
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
nil
,
err
return
nil
,
nil
,
err
...
@@ -251,7 +257,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
...
@@ -251,7 +257,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
}
}
// If the deserializer is non-strict, return successfully here.
// If the deserializer is non-strict, return successfully here.
if
!
s
.
Strict
{
if
!
s
.
options
.
Strict
{
return
obj
,
actual
,
nil
return
obj
,
actual
,
nil
}
}
...
@@ -280,7 +286,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
...
@@ -280,7 +286,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
// Encode serializes the provided object to the given writer.
// Encode serializes the provided object to the given writer.
func
(
s
*
Serializer
)
Encode
(
obj
runtime
.
Object
,
w
io
.
Writer
)
error
{
func
(
s
*
Serializer
)
Encode
(
obj
runtime
.
Object
,
w
io
.
Writer
)
error
{
if
s
.
Yaml
{
if
s
.
options
.
Yaml
{
json
,
err
:=
caseSensitiveJsonIterator
.
Marshal
(
obj
)
json
,
err
:=
caseSensitiveJsonIterator
.
Marshal
(
obj
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
...
@@ -293,7 +299,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
...
@@ -293,7 +299,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
return
err
return
err
}
}
if
s
.
Pretty
{
if
s
.
options
.
Pretty
{
data
,
err
:=
caseSensitiveJsonIterator
.
MarshalIndent
(
obj
,
""
,
" "
)
data
,
err
:=
caseSensitiveJsonIterator
.
MarshalIndent
(
obj
,
""
,
" "
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
...
@@ -307,7 +313,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
...
@@ -307,7 +313,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
// RecognizesData implements the RecognizingDecoder interface.
// RecognizesData implements the RecognizingDecoder interface.
func
(
s
*
Serializer
)
RecognizesData
(
peek
io
.
Reader
)
(
ok
,
unknown
bool
,
err
error
)
{
func
(
s
*
Serializer
)
RecognizesData
(
peek
io
.
Reader
)
(
ok
,
unknown
bool
,
err
error
)
{
if
s
.
Yaml
{
if
s
.
options
.
Yaml
{
// we could potentially look for '---'
// we could potentially look for '---'
return
false
,
true
,
nil
return
false
,
true
,
nil
}
}
...
...
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go
View file @
f5de14a6
...
@@ -418,9 +418,9 @@ func TestDecode(t *testing.T) {
...
@@ -418,9 +418,9 @@ func TestDecode(t *testing.T) {
for
i
,
test
:=
range
testCases
{
for
i
,
test
:=
range
testCases
{
var
s
runtime
.
Serializer
var
s
runtime
.
Serializer
if
test
.
yaml
{
if
test
.
yaml
{
s
=
json
.
NewSerializerWithOptions
(
json
.
DefaultMetaFactory
,
test
.
creater
,
test
.
typer
,
&
json
.
SerializerOptions
{
Yaml
:
test
.
yaml
,
Pretty
:
false
,
Strict
:
test
.
strict
})
s
=
json
.
NewSerializerWithOptions
(
json
.
DefaultMetaFactory
,
test
.
creater
,
test
.
typer
,
json
.
SerializerOptions
{
Yaml
:
test
.
yaml
,
Pretty
:
false
,
Strict
:
test
.
strict
})
}
else
{
}
else
{
s
=
json
.
NewSerializerWithOptions
(
json
.
DefaultMetaFactory
,
test
.
creater
,
test
.
typer
,
&
json
.
SerializerOptions
{
Yaml
:
test
.
yaml
,
Pretty
:
test
.
pretty
,
Strict
:
test
.
strict
})
s
=
json
.
NewSerializerWithOptions
(
json
.
DefaultMetaFactory
,
test
.
creater
,
test
.
typer
,
json
.
SerializerOptions
{
Yaml
:
test
.
yaml
,
Pretty
:
test
.
pretty
,
Strict
:
test
.
strict
})
}
}
obj
,
gvk
,
err
:=
s
.
Decode
([]
byte
(
test
.
data
),
test
.
defaultGVK
,
test
.
into
)
obj
,
gvk
,
err
:=
s
.
Decode
([]
byte
(
test
.
data
),
test
.
defaultGVK
,
test
.
into
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment