Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
5166f511
Commit
5166f511
authored
Aug 09, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common/json: add Unmarshal with method with syntax errors
parent
51cfc355
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
28 deletions
+45
-28
common/command/template.go
common/command/template.go
+2
-2
common/json/unmarshal.go
common/json/unmarshal.go
+40
-0
common/json/unmarshal_test.go
common/json/unmarshal_test.go
+1
-0
packer/template.go
packer/template.go
+2
-26
No files found.
common/command/template.go
View file @
5166f511
package
command
import
(
"encoding/json"
"errors"
"fmt"
jsonutil
"github.com/mitchellh/packer/common/json"
"github.com/mitchellh/packer/packer"
"io/ioutil"
"log"
...
...
@@ -116,7 +116,7 @@ func readFileVars(path string) (map[string]string, error) {
}
vars
:=
make
(
map
[
string
]
string
)
err
=
json
.
Unmarshal
(
bytes
,
&
vars
)
err
=
json
util
.
Unmarshal
(
bytes
,
&
vars
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
common/json/unmarshal.go
0 → 100644
View file @
5166f511
package
json
import
(
"bytes"
"encoding/json"
"fmt"
)
// Unmarshal is wrapper around json.Unmarshal that returns user-friendly
// errors when there are syntax errors.
func
Unmarshal
(
data
[]
byte
,
i
interface
{})
error
{
err
:=
json
.
Unmarshal
(
data
,
i
)
if
err
!=
nil
{
syntaxErr
,
ok
:=
err
.
(
*
json
.
SyntaxError
)
if
!
ok
{
return
err
}
// We have a syntax error. Extract out the line number and friends.
// https://groups.google.com/forum/#!topic/golang-nuts/fizimmXtVfc
newline
:=
[]
byte
{
'\x0a'
}
// Calculate the start/end position of the line where the error is
start
:=
bytes
.
LastIndex
(
data
[
:
syntaxErr
.
Offset
],
newline
)
+
1
end
:=
len
(
data
)
if
idx
:=
bytes
.
Index
(
data
[
start
:
],
newline
);
idx
>=
0
{
end
=
start
+
idx
}
// Count the line number we're on plus the offset in the line
line
:=
bytes
.
Count
(
data
[
:
start
],
newline
)
+
1
pos
:=
int
(
syntaxErr
.
Offset
)
-
start
-
1
err
=
fmt
.
Errorf
(
"Error in line %d, char %d: %s
\n
%s"
,
line
,
pos
,
syntaxErr
,
data
[
start
:
end
])
return
err
}
return
nil
}
common/json/unmarshal_test.go
0 → 100644
View file @
5166f511
package
json
packer/template.go
View file @
5166f511
package
packer
import
(
"bytes"
"encoding/json"
"fmt"
"github.com/mitchellh/mapstructure"
jsonutil
"github.com/mitchellh/packer/common/json"
"io/ioutil"
"sort"
)
...
...
@@ -68,31 +67,8 @@ type rawProvisionerConfig struct {
// way.
func
ParseTemplate
(
data
[]
byte
)
(
t
*
Template
,
err
error
)
{
var
rawTplInterface
interface
{}
err
=
json
.
Unmarshal
(
data
,
&
rawTplInterface
)
err
=
json
util
.
Unmarshal
(
data
,
&
rawTplInterface
)
if
err
!=
nil
{
syntaxErr
,
ok
:=
err
.
(
*
json
.
SyntaxError
)
if
!
ok
{
return
}
// We have a syntax error. Extract out the line number and friends.
// https://groups.google.com/forum/#!topic/golang-nuts/fizimmXtVfc
newline
:=
[]
byte
{
'\x0a'
}
// Calculate the start/end position of the line where the error is
start
:=
bytes
.
LastIndex
(
data
[
:
syntaxErr
.
Offset
],
newline
)
+
1
end
:=
len
(
data
)
if
idx
:=
bytes
.
Index
(
data
[
start
:
],
newline
);
idx
>=
0
{
end
=
start
+
idx
}
// Count the line number we're on plus the offset in the line
line
:=
bytes
.
Count
(
data
[
:
start
],
newline
)
+
1
pos
:=
int
(
syntaxErr
.
Offset
)
-
start
-
1
err
=
fmt
.
Errorf
(
"Error in line %d, char %d: %s
\n
%s"
,
line
,
pos
,
syntaxErr
,
data
[
start
:
end
])
return
}
...
...
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