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
93d45c1e
Commit
93d45c1e
authored
Jul 11, 2014
by
brendandburns
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #414 from thockin/cleanups
Fix go idiom feedback
parents
34edf290
7d5398bd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
84 deletions
+46
-84
util.go
pkg/util/util.go
+6
-6
util_test.go
pkg/util/util_test.go
+40
-78
No files found.
pkg/util/util.go
View file @
93d45c1e
...
...
@@ -85,14 +85,14 @@ const (
// SetYAML implements the yaml.Setter interface.
func
(
intstr
*
IntOrString
)
SetYAML
(
tag
string
,
value
interface
{})
bool
{
if
intVal
,
ok
:=
value
.
(
int
);
ok
{
switch
v
:=
value
.
(
type
)
{
case
int
:
intstr
.
Kind
=
IntstrInt
intstr
.
IntVal
=
intVal
intstr
.
IntVal
=
v
return
true
}
if
strVal
,
ok
:=
value
.
(
string
);
ok
{
case
string
:
intstr
.
Kind
=
IntstrString
intstr
.
StrVal
=
strVal
intstr
.
StrVal
=
v
return
true
}
return
false
...
...
@@ -129,6 +129,6 @@ func (intstr IntOrString) MarshalJSON() ([]byte, error) {
case
IntstrString
:
return
json
.
Marshal
(
intstr
.
StrVal
)
default
:
panic
(
"impossible IntOrString.Kind"
)
return
[]
byte
{},
fmt
.
Errorf
(
"impossible IntOrString.Kind"
)
}
}
pkg/util/util_test.go
View file @
93d45c1e
...
...
@@ -73,121 +73,83 @@ type IntOrStringHolder struct {
}
func
TestIntOrStringUnmarshalYAML
(
t
*
testing
.
T
)
{
{
yamlCodeInt
:=
"val: 123
\n
"
var
result
IntOrStringHolder
if
err
:=
yaml
.
Unmarshal
([]
byte
(
yamlCodeInt
),
&
result
);
err
!=
nil
{
t
.
Errorf
(
"Failed to unmarshal: %v"
,
err
)
}
if
result
.
IOrS
.
Kind
!=
IntstrInt
||
result
.
IOrS
.
IntVal
!=
123
{
t
.
Errorf
(
"Failed to unmarshal int-typed IntOrString: %v"
,
result
)
}
cases
:=
[]
struct
{
input
string
result
IntOrString
}{
{
"val: 123
\n
"
,
IntOrString
{
Kind
:
IntstrInt
,
IntVal
:
123
}},
{
"val:
\"
123
\"\n
"
,
IntOrString
{
Kind
:
IntstrString
,
StrVal
:
"123"
}},
}
{
yamlCodeStr
:=
"val:
\"
123
\"\n
"
for
_
,
c
:=
range
cases
{
var
result
IntOrStringHolder
if
err
:=
yaml
.
Unmarshal
([]
byte
(
yamlCodeStr
),
&
result
);
err
!=
nil
{
if
err
:=
yaml
.
Unmarshal
([]
byte
(
c
.
input
),
&
result
);
err
!=
nil
{
t
.
Errorf
(
"Failed to unmarshal: %v"
,
err
)
}
if
result
.
IOrS
.
Kind
!=
IntstrString
||
result
.
IOrS
.
StrVal
!=
"123"
{
t
.
Errorf
(
"Failed to unmarshal
string-typed IntOrString: %
v"
,
result
)
if
result
.
IOrS
!=
c
.
result
{
t
.
Errorf
(
"Failed to unmarshal
IntOrString: got %+
v"
,
result
)
}
}
}
func
TestIntOrStringMarshalYAML
(
t
*
testing
.
T
)
{
{
input
:=
IntOrStringHolder
{
IOrS
:
IntOrString
{
Kind
:
IntstrInt
,
IntVal
:
123
,
},
}
result
,
err
:=
yaml
.
Marshal
(
&
input
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to marshal: %v"
,
err
)
}
if
string
(
result
)
!=
"val: 123
\n
"
{
t
.
Errorf
(
"Failed to marshal int-typed IntOrString: %q"
,
string
(
result
))
}
cases
:=
[]
struct
{
input
IntOrString
result
string
}{
{
IntOrString
{
Kind
:
IntstrInt
,
IntVal
:
123
},
"val: 123
\n
"
},
{
IntOrString
{
Kind
:
IntstrString
,
StrVal
:
"123"
},
"val:
\"
123
\"\n
"
},
}
{
input
:=
IntOrStringHolder
{
IOrS
:
IntOrString
{
Kind
:
IntstrString
,
StrVal
:
"123"
,
},
}
for
_
,
c
:=
range
cases
{
input
:=
IntOrStringHolder
{
c
.
input
}
result
,
err
:=
yaml
.
Marshal
(
&
input
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to marshal: %v"
,
err
)
}
if
string
(
result
)
!=
"val:
\"
123
\"\n
"
{
t
.
Errorf
(
"Failed to marshal
string-typed IntOrString:
%q"
,
string
(
result
))
if
string
(
result
)
!=
c
.
result
{
t
.
Errorf
(
"Failed to marshal
IntOrString: got
%q"
,
string
(
result
))
}
}
}
func
TestIntOrStringUnmarshalJSON
(
t
*
testing
.
T
)
{
{
jsonCodeInt
:=
"{
\"
val
\"
: 123}"
var
result
IntOrStringHolder
if
err
:=
json
.
Unmarshal
([]
byte
(
jsonCodeInt
),
&
result
);
err
!=
nil
{
t
.
Errorf
(
"Failed to unmarshal: %v"
,
err
)
}
if
result
.
IOrS
.
Kind
!=
IntstrInt
||
result
.
IOrS
.
IntVal
!=
123
{
t
.
Errorf
(
"Failed to unmarshal int-typed IntOrString: %v"
,
result
)
}
cases
:=
[]
struct
{
input
string
result
IntOrString
}{
{
"{
\"
val
\"
: 123}"
,
IntOrString
{
Kind
:
IntstrInt
,
IntVal
:
123
}},
{
"{
\"
val
\"
:
\"
123
\"
}"
,
IntOrString
{
Kind
:
IntstrString
,
StrVal
:
"123"
}},
}
{
jsonCodeStr
:=
"{
\"
val
\"
:
\"
123
\"
}"
for
_
,
c
:=
range
cases
{
var
result
IntOrStringHolder
if
err
:=
json
.
Unmarshal
([]
byte
(
jsonCodeStr
),
&
result
);
err
!=
nil
{
if
err
:=
json
.
Unmarshal
([]
byte
(
c
.
input
),
&
result
);
err
!=
nil
{
t
.
Errorf
(
"Failed to unmarshal: %v"
,
err
)
}
if
result
.
IOrS
.
Kind
!=
IntstrString
||
result
.
IOrS
.
StrVal
!=
"123"
{
t
.
Errorf
(
"Failed to unmarshal
string-typed IntOrString: %
v"
,
result
)
if
result
.
IOrS
!=
c
.
result
{
t
.
Errorf
(
"Failed to unmarshal
IntOrString: got %+
v"
,
result
)
}
}
}
func
TestIntOrStringMarshalJSON
(
t
*
testing
.
T
)
{
{
input
:=
IntOrStringHolder
{
IOrS
:
IntOrString
{
Kind
:
IntstrInt
,
IntVal
:
123
,
},
}
result
,
err
:=
json
.
Marshal
(
&
input
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to marshal: %v"
,
err
)
}
if
string
(
result
)
!=
"{
\"
val
\"
:123}"
{
t
.
Errorf
(
"Failed to marshal int-typed IntOrString: %q"
,
string
(
result
))
}
cases
:=
[]
struct
{
input
IntOrString
result
string
}{
{
IntOrString
{
Kind
:
IntstrInt
,
IntVal
:
123
},
"{
\"
val
\"
:123}"
},
{
IntOrString
{
Kind
:
IntstrString
,
StrVal
:
"123"
},
"{
\"
val
\"
:
\"
123
\"
}"
},
}
{
input
:=
IntOrStringHolder
{
IOrS
:
IntOrString
{
Kind
:
IntstrString
,
StrVal
:
"123"
,
},
}
for
_
,
c
:=
range
cases
{
input
:=
IntOrStringHolder
{
c
.
input
}
result
,
err
:=
json
.
Marshal
(
&
input
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to marshal: %v"
,
err
)
}
if
string
(
result
)
!=
"{
\"
val
\"
:
\"
123
\"
}"
{
t
.
Errorf
(
"Failed to marshal
string-typed IntOrString:
%q"
,
string
(
result
))
if
string
(
result
)
!=
c
.
result
{
t
.
Errorf
(
"Failed to marshal
IntOrString: got
%q"
,
string
(
result
))
}
}
}
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