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
71abc99d
Commit
71abc99d
authored
Mar 16, 2015
by
Clayton Coleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Expose a ToJSON and runtime.YAMLDecoder helper
Enables clients to optionally handle YAML
parent
022c1036
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
2 deletions
+57
-2
codec.go
pkg/runtime/codec.go
+36
-0
decoder.go
pkg/util/yaml/decoder.go
+21
-2
No files found.
pkg/runtime/codec.go
View file @
71abc99d
...
@@ -16,11 +16,47 @@ limitations under the License.
...
@@ -16,11 +16,47 @@ limitations under the License.
package
runtime
package
runtime
import
(
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/yaml"
)
// CodecFor returns a Codec that invokes Encode with the provided version.
// CodecFor returns a Codec that invokes Encode with the provided version.
func
CodecFor
(
scheme
*
Scheme
,
version
string
)
Codec
{
func
CodecFor
(
scheme
*
Scheme
,
version
string
)
Codec
{
return
&
codecWrapper
{
scheme
,
version
}
return
&
codecWrapper
{
scheme
,
version
}
}
}
// yamlCodec converts YAML passed to the Decoder methods to JSON.
type
yamlCodec
struct
{
// a Codec for JSON
Codec
}
// yamlCodec implements Codec
var
_
Codec
=
yamlCodec
{}
// YAMLDecoder adds YAML decoding support to a codec that supports JSON.
func
YAMLDecoder
(
codec
Codec
)
Codec
{
return
&
yamlCodec
{
codec
}
}
func
(
c
yamlCodec
)
Decode
(
data
[]
byte
)
(
Object
,
error
)
{
out
,
err
:=
yaml
.
ToJSON
(
data
)
if
err
!=
nil
{
return
nil
,
err
}
data
=
out
return
c
.
Codec
.
Decode
(
data
)
}
func
(
c
yamlCodec
)
DecodeInto
(
data
[]
byte
,
obj
Object
)
error
{
out
,
err
:=
yaml
.
ToJSON
(
data
)
if
err
!=
nil
{
return
err
}
data
=
out
return
c
.
Codec
.
DecodeInto
(
data
,
obj
)
}
// EncodeOrDie is a version of Encode which will panic instead of returning an error. For tests.
// EncodeOrDie is a version of Encode which will panic instead of returning an error. For tests.
func
EncodeOrDie
(
codec
Codec
,
obj
Object
)
string
{
func
EncodeOrDie
(
codec
Codec
,
obj
Object
)
string
{
bytes
,
err
:=
codec
.
Encode
(
obj
)
bytes
,
err
:=
codec
.
Encode
(
obj
)
...
...
pkg/util/yaml/decoder.go
View file @
71abc99d
...
@@ -27,6 +27,17 @@ import (
...
@@ -27,6 +27,17 @@ import (
"github.com/golang/glog"
"github.com/golang/glog"
)
)
// ToJSON converts a single YAML document into a JSON document
// or returns an error. If the document appears to be JSON the
// YAML decoding path is not used (so that error messages are)
// JSON specific.
func
ToJSON
(
data
[]
byte
)
([]
byte
,
error
)
{
if
hasJSONPrefix
(
data
)
{
return
data
,
nil
}
return
yaml
.
YAMLToJSON
(
data
)
}
// YAMLToJSONDecoder decodes YAML documents from an io.Reader by
// YAMLToJSONDecoder decodes YAML documents from an io.Reader by
// separating individual documents. It first converts the YAML
// separating individual documents. It first converts the YAML
// body to JSON, then unmarshals the JSON.
// body to JSON, then unmarshals the JSON.
...
@@ -143,11 +154,19 @@ func (d *YAMLOrJSONDecoder) Decode(into interface{}) error {
...
@@ -143,11 +154,19 @@ func (d *YAMLOrJSONDecoder) Decode(into interface{}) error {
func
guessJSONStream
(
r
io
.
Reader
,
size
int
)
(
io
.
Reader
,
bool
)
{
func
guessJSONStream
(
r
io
.
Reader
,
size
int
)
(
io
.
Reader
,
bool
)
{
buffer
:=
bufio
.
NewReaderSize
(
r
,
size
)
buffer
:=
bufio
.
NewReaderSize
(
r
,
size
)
b
,
_
:=
buffer
.
Peek
(
size
)
b
,
_
:=
buffer
.
Peek
(
size
)
return
buffer
,
hasPrefix
(
b
,
[]
byte
(
"{"
))
return
buffer
,
hasJSONPrefix
(
b
)
}
var
jsonPrefix
=
[]
byte
(
"{"
)
// hasJSONPrefix returns true if the provided buffer appears to start with
// a JSON open brace.
func
hasJSONPrefix
(
buf
[]
byte
)
bool
{
return
hasPrefix
(
buf
,
jsonPrefix
)
}
}
// Return true if the first non-whitespace bytes in buf is
// Return true if the first non-whitespace bytes in buf is
// prefix
// prefix
.
func
hasPrefix
(
buf
[]
byte
,
prefix
[]
byte
)
bool
{
func
hasPrefix
(
buf
[]
byte
,
prefix
[]
byte
)
bool
{
trim
:=
bytes
.
TrimLeftFunc
(
buf
,
unicode
.
IsSpace
)
trim
:=
bytes
.
TrimLeftFunc
(
buf
,
unicode
.
IsSpace
)
return
bytes
.
HasPrefix
(
trim
,
prefix
)
return
bytes
.
HasPrefix
(
trim
,
prefix
)
...
...
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