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
543a8b2d
Unverified
Commit
543a8b2d
authored
May 02, 2016
by
Michal Fojtik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Display line number on JSON errors
parent
c66b2b4e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
1 deletion
+53
-1
decoder.go
pkg/util/yaml/decoder.go
+17
-1
decoder_test.go
pkg/util/yaml/decoder_test.go
+36
-0
No files found.
pkg/util/yaml/decoder.go
View file @
543a8b2d
...
...
@@ -20,7 +20,10 @@ import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"unicode"
"github.com/ghodss/yaml"
...
...
@@ -203,7 +206,20 @@ func (d *YAMLOrJSONDecoder) Decode(into interface{}) error {
d
.
decoder
=
NewYAMLToJSONDecoder
(
buffer
)
}
}
return
d
.
decoder
.
Decode
(
into
)
err
:=
d
.
decoder
.
Decode
(
into
)
if
jsonDecoder
,
ok
:=
d
.
decoder
.
(
*
json
.
Decoder
);
ok
{
if
syntax
,
ok
:=
err
.
(
*
json
.
SyntaxError
);
ok
{
data
,
readErr
:=
ioutil
.
ReadAll
(
jsonDecoder
.
Buffered
())
if
readErr
!=
nil
{
glog
.
V
(
4
)
.
Infof
(
"reading stream failed: %v"
,
readErr
)
}
js
:=
string
(
data
)
start
:=
strings
.
LastIndex
(
js
[
:
syntax
.
Offset
],
"
\n
"
)
+
1
line
:=
strings
.
Count
(
js
[
:
start
],
"
\n
"
)
return
fmt
.
Errorf
(
"json: line %d: %s"
,
line
,
syntax
.
Error
())
}
}
return
err
}
// GuessJSONStream scans the provided reader up to size, looking
...
...
pkg/util/yaml/decoder_test.go
View file @
543a8b2d
...
...
@@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"io"
"strings"
"testing"
)
...
...
@@ -125,6 +126,41 @@ stuff: 1
}
}
func
TestDecodeBrokenYAML
(
t
*
testing
.
T
)
{
s
:=
NewYAMLOrJSONDecoder
(
bytes
.
NewReader
([]
byte
(
`---
stuff: 1
test-foo: 1
---
`
)),
100
)
obj
:=
generic
{}
err
:=
s
.
Decode
(
&
obj
)
if
err
==
nil
{
t
.
Fatal
(
"expected error with yaml: prefix, got no error"
)
}
if
!
strings
.
HasPrefix
(
err
.
Error
(),
"yaml: line 2:"
)
{
t
.
Fatalf
(
"expected %q to have 'yaml: line 2:' prefix"
,
err
.
Error
())
}
}
func
TestDecodeBrokenJSON
(
t
*
testing
.
T
)
{
s
:=
NewYAMLOrJSONDecoder
(
bytes
.
NewReader
([]
byte
(
`{
"foo": {
"stuff": 1
"otherStuff": 2
}
}
`
)),
100
)
obj
:=
generic
{}
err
:=
s
.
Decode
(
&
obj
)
if
err
==
nil
{
t
.
Fatal
(
"expected error with json: prefix, got no error"
)
}
if
!
strings
.
HasPrefix
(
err
.
Error
(),
"json: line 3:"
)
{
t
.
Fatalf
(
"expected %q to have 'json: line 3:' prefix"
,
err
.
Error
())
}
}
type
generic
map
[
string
]
interface
{}
func
TestYAMLOrJSONDecoder
(
t
*
testing
.
T
)
{
...
...
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