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
102eced0
Commit
102eced0
authored
Nov 05, 2015
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce a FieldPath type for validation
parent
f9715c64
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
214 additions
and
0 deletions
+214
-0
fieldpath.go
pkg/util/validation/fieldpath.go
+91
-0
fieldpath_test.go
pkg/util/validation/fieldpath_test.go
+123
-0
No files found.
pkg/util/validation/fieldpath.go
0 → 100644
View file @
102eced0
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
validation
import
(
"bytes"
"fmt"
"strconv"
)
// FieldPath represents the path from some root to a particular field.
type
FieldPath
struct
{
name
string
// the name of this field or "" if this is an index
index
string
// if name == "", this is a subscript (index or map key) of the previous element
parent
*
FieldPath
// nil if this is the root element
}
// NewFieldPath creates a root FieldPath object.
func
NewFieldPath
(
name
string
,
moreNames
...
string
)
*
FieldPath
{
r
:=
&
FieldPath
{
name
:
name
,
parent
:
nil
}
for
_
,
anotherName
:=
range
moreNames
{
r
=
&
FieldPath
{
name
:
anotherName
,
parent
:
r
}
}
return
r
}
// Root returns the root element of this FieldPath.
func
(
fp
*
FieldPath
)
Root
()
*
FieldPath
{
for
;
fp
.
parent
!=
nil
;
fp
=
fp
.
parent
{
// Do nothing.
}
return
fp
}
// Child creates a new FieldPath that is a child of the method receiver.
func
(
fp
*
FieldPath
)
Child
(
name
string
,
moreNames
...
string
)
*
FieldPath
{
r
:=
NewFieldPath
(
name
,
moreNames
...
)
r
.
Root
()
.
parent
=
fp
return
r
}
// Index indicates that the previous FieldPath is to be subscripted by an int.
// This sets the same underlying value as Key.
func
(
fp
*
FieldPath
)
Index
(
index
int
)
*
FieldPath
{
return
&
FieldPath
{
index
:
strconv
.
Itoa
(
index
),
parent
:
fp
}
}
// Key indicates that the previous FieldPath is to be subscripted by a string.
// This sets the same underlying value as Index.
func
(
fp
*
FieldPath
)
Key
(
key
string
)
*
FieldPath
{
return
&
FieldPath
{
index
:
key
,
parent
:
fp
}
}
// String produces a string representation of the FieldPath.
func
(
fp
*
FieldPath
)
String
()
string
{
// make a slice to iterate
elems
:=
[]
*
FieldPath
{}
for
p
:=
fp
;
p
!=
nil
;
p
=
p
.
parent
{
elems
=
append
(
elems
,
p
)
}
// iterate, but it has to be backwards
buf
:=
bytes
.
NewBuffer
(
nil
)
for
i
:=
range
elems
{
p
:=
elems
[
len
(
elems
)
-
1
-
i
]
if
p
.
parent
!=
nil
&&
len
(
p
.
name
)
>
0
{
// This is either the root or it is a subscript.
buf
.
WriteString
(
"."
)
}
if
len
(
p
.
name
)
>
0
{
buf
.
WriteString
(
p
.
name
)
}
else
{
fmt
.
Fprintf
(
buf
,
"[%s]"
,
p
.
index
)
}
}
return
buf
.
String
()
}
pkg/util/validation/fieldpath_test.go
0 → 100644
View file @
102eced0
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
validation
import
"testing"
func
TestFieldPath
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
op
func
(
*
FieldPath
)
*
FieldPath
expected
string
}{
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
},
"root"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
Child
(
"first"
)
},
"root.first"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
Child
(
"second"
)
},
"root.first.second"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
Index
(
0
)
},
"root.first.second[0]"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
Child
(
"third"
)
},
"root.first.second[0].third"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
Index
(
93
)
},
"root.first.second[0].third[93]"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
parent
},
"root.first.second[0].third"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
parent
},
"root.first.second[0]"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
Key
(
"key"
)
},
"root.first.second[0][key]"
,
},
}
root
:=
NewFieldPath
(
"root"
)
fp
:=
root
for
i
,
tc
:=
range
testCases
{
fp
=
tc
.
op
(
fp
)
if
fp
.
String
()
!=
tc
.
expected
{
t
.
Errorf
(
"[%d] Expected %q, got %q"
,
i
,
tc
.
expected
,
fp
.
String
())
}
if
fp
.
Root
()
!=
root
{
t
.
Errorf
(
"[%d] Wrong root: %#v"
,
i
,
fp
.
Root
())
}
}
}
func
TestFieldPathMultiArg
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
op
func
(
*
FieldPath
)
*
FieldPath
expected
string
}{
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
},
"root.first"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
Child
(
"second"
,
"third"
)
},
"root.first.second.third"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
Index
(
0
)
},
"root.first.second.third[0]"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
parent
},
"root.first.second.third"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
parent
},
"root.first.second"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
parent
},
"root.first"
,
},
{
func
(
fp
*
FieldPath
)
*
FieldPath
{
return
fp
.
parent
},
"root"
,
},
}
root
:=
NewFieldPath
(
"root"
,
"first"
)
fp
:=
root
for
i
,
tc
:=
range
testCases
{
fp
=
tc
.
op
(
fp
)
if
fp
.
String
()
!=
tc
.
expected
{
t
.
Errorf
(
"[%d] Expected %q, got %q"
,
i
,
tc
.
expected
,
fp
.
String
())
}
if
fp
.
Root
()
!=
root
.
Root
()
{
t
.
Errorf
(
"[%d] Wrong root: %#v"
,
i
,
fp
.
Root
())
}
}
}
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