mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 01:38:10 +00:00
vendor dependencies
This commit is contained in:
parent
604208ef4f
commit
72abf135d6
1156 changed files with 78178 additions and 105799 deletions
201
vendor/sigs.k8s.io/structured-merge-diff/LICENSE
generated
vendored
Normal file
201
vendor/sigs.k8s.io/structured-merge-diff/LICENSE
generated
vendored
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
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.
|
||||
21
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/doc.go
generated
vendored
Normal file
21
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/doc.go
generated
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 fieldpath defines a way for referencing path elements (e.g., an
|
||||
// index in an array, or a key in a map). It provides types for arranging these
|
||||
// into paths for referencing nested fields, and for grouping those into sets,
|
||||
// for referencing multiple nested fields.
|
||||
package fieldpath
|
||||
184
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/element.go
generated
vendored
Normal file
184
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/element.go
generated
vendored
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 fieldpath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
// PathElement describes how to select a child field given a containing object.
|
||||
type PathElement struct {
|
||||
// Exactly one of the following fields should be non-nil.
|
||||
|
||||
// FieldName selects a single field from a map (reminder: this is also
|
||||
// how structs are represented). The containing object must be a map.
|
||||
FieldName *string
|
||||
|
||||
// Key selects the list element which has fields matching those given.
|
||||
// The containing object must be an associative list with map typed
|
||||
// elements.
|
||||
Key []value.Field
|
||||
|
||||
// Value selects the list element with the given value. The containing
|
||||
// object must be an associative list with a primitive typed element
|
||||
// (i.e., a set).
|
||||
Value *value.Value
|
||||
|
||||
// Index selects a list element by its index number. The containing
|
||||
// object must be an atomic list.
|
||||
Index *int
|
||||
}
|
||||
|
||||
// String presents the path element as a human-readable string.
|
||||
func (e PathElement) String() string {
|
||||
switch {
|
||||
case e.FieldName != nil:
|
||||
return "." + *e.FieldName
|
||||
case len(e.Key) > 0:
|
||||
strs := make([]string, len(e.Key))
|
||||
for i, k := range e.Key {
|
||||
strs[i] = fmt.Sprintf("%v=%v", k.Name, k.Value)
|
||||
}
|
||||
// The order must be canonical, since we use the string value
|
||||
// in a set structure.
|
||||
sort.Strings(strs)
|
||||
return "[" + strings.Join(strs, ",") + "]"
|
||||
case e.Value != nil:
|
||||
return fmt.Sprintf("[=%v]", e.Value)
|
||||
case e.Index != nil:
|
||||
return fmt.Sprintf("[%v]", *e.Index)
|
||||
default:
|
||||
return "{{invalid path element}}"
|
||||
}
|
||||
}
|
||||
|
||||
// KeyByFields is a helper function which constructs a key for an associative
|
||||
// list type. `nameValues` must have an even number of entries, alternating
|
||||
// names (type must be string) with values (type must be value.Value). If these
|
||||
// conditions are not met, KeyByFields will panic--it's intended for static
|
||||
// construction and shouldn't have user-produced values passed to it.
|
||||
func KeyByFields(nameValues ...interface{}) []value.Field {
|
||||
if len(nameValues)%2 != 0 {
|
||||
panic("must have a value for every name")
|
||||
}
|
||||
out := []value.Field{}
|
||||
for i := 0; i < len(nameValues)-1; i += 2 {
|
||||
out = append(out, value.Field{
|
||||
Name: nameValues[i].(string),
|
||||
Value: nameValues[i+1].(value.Value),
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// PathElementSet is a set of path elements.
|
||||
// TODO: serialize as a list.
|
||||
type PathElementSet struct {
|
||||
// The strange construction is because there's no way to test
|
||||
// PathElements for equality (it can't be used as a key for a map).
|
||||
members map[string]PathElement
|
||||
}
|
||||
|
||||
// Insert adds pe to the set.
|
||||
func (s *PathElementSet) Insert(pe PathElement) {
|
||||
serialized := pe.String()
|
||||
if s.members == nil {
|
||||
s.members = map[string]PathElement{
|
||||
serialized: pe,
|
||||
}
|
||||
return
|
||||
}
|
||||
if _, ok := s.members[serialized]; !ok {
|
||||
s.members[serialized] = pe
|
||||
}
|
||||
}
|
||||
|
||||
// Union returns a set containing elements that appear in either s or s2.
|
||||
func (s *PathElementSet) Union(s2 *PathElementSet) *PathElementSet {
|
||||
out := &PathElementSet{
|
||||
members: map[string]PathElement{},
|
||||
}
|
||||
for k, v := range s.members {
|
||||
out.members[k] = v
|
||||
}
|
||||
for k, v := range s2.members {
|
||||
out.members[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Intersection returns a set containing elements which appear in both s and s2.
|
||||
func (s *PathElementSet) Intersection(s2 *PathElementSet) *PathElementSet {
|
||||
out := &PathElementSet{
|
||||
members: map[string]PathElement{},
|
||||
}
|
||||
for k, v := range s.members {
|
||||
if _, ok := s2.members[k]; ok {
|
||||
out.members[k] = v
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Difference returns a set containing elements which appear in s but not in s2.
|
||||
func (s *PathElementSet) Difference(s2 *PathElementSet) *PathElementSet {
|
||||
out := &PathElementSet{
|
||||
members: map[string]PathElement{},
|
||||
}
|
||||
for k, v := range s.members {
|
||||
if _, ok := s2.members[k]; !ok {
|
||||
out.members[k] = v
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Size retuns the number of elements in the set.
|
||||
func (s *PathElementSet) Size() int { return len(s.members) }
|
||||
|
||||
// Has returns true if pe is a member of the set.
|
||||
func (s *PathElementSet) Has(pe PathElement) bool {
|
||||
if s.members == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := s.members[pe.String()]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Equals returns true if s and s2 have exactly the same members.
|
||||
func (s *PathElementSet) Equals(s2 *PathElementSet) bool {
|
||||
if len(s.members) != len(s2.members) {
|
||||
return false
|
||||
}
|
||||
for k := range s.members {
|
||||
if _, ok := s2.members[k]; !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Iterate calls f for each PathElement in the set.
|
||||
func (s *PathElementSet) Iterate(f func(PathElement)) {
|
||||
for _, pe := range s.members {
|
||||
f(pe)
|
||||
}
|
||||
}
|
||||
123
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/fromvalue.go
generated
vendored
Normal file
123
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/fromvalue.go
generated
vendored
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 fieldpath
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
// SetFromValue creates a set containing every leaf field mentioned in v.
|
||||
func SetFromValue(v value.Value) *Set {
|
||||
s := NewSet()
|
||||
|
||||
w := objectWalker{
|
||||
path: Path{},
|
||||
value: v,
|
||||
do: func(p Path) { s.Insert(p) },
|
||||
}
|
||||
|
||||
w.walk()
|
||||
return s
|
||||
}
|
||||
|
||||
type objectWalker struct {
|
||||
path Path
|
||||
value value.Value
|
||||
|
||||
do func(Path)
|
||||
}
|
||||
|
||||
func (w *objectWalker) walk() {
|
||||
switch {
|
||||
case w.value.Null:
|
||||
case w.value.FloatValue != nil:
|
||||
case w.value.IntValue != nil:
|
||||
case w.value.StringValue != nil:
|
||||
case w.value.BooleanValue != nil:
|
||||
// All leaf fields handled the same way (after the switch
|
||||
// statement).
|
||||
|
||||
// Descend
|
||||
case w.value.ListValue != nil:
|
||||
// If the list were atomic, we'd break here, but we don't have
|
||||
// a schema, so we can't tell.
|
||||
|
||||
for i, child := range w.value.ListValue.Items {
|
||||
w2 := *w
|
||||
w2.path = append(w.path, GuessBestListPathElement(i, child))
|
||||
w2.value = child
|
||||
w2.walk()
|
||||
}
|
||||
return
|
||||
case w.value.MapValue != nil:
|
||||
// If the map/struct were atomic, we'd break here, but we don't
|
||||
// have a schema, so we can't tell.
|
||||
|
||||
for i := range w.value.MapValue.Items {
|
||||
child := w.value.MapValue.Items[i]
|
||||
w2 := *w
|
||||
w2.path = append(w.path, PathElement{FieldName: &child.Name})
|
||||
w2.value = child.Value
|
||||
w2.walk()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Leaf fields get added to the set.
|
||||
if len(w.path) > 0 {
|
||||
w.do(w.path)
|
||||
}
|
||||
}
|
||||
|
||||
// AssociativeListCandidateFieldNames lists the field names which are
|
||||
// considered keys if found in a list element.
|
||||
var AssociativeListCandidateFieldNames = []string{
|
||||
"key",
|
||||
"id",
|
||||
"name",
|
||||
}
|
||||
|
||||
// GuessBestListPathElement guesses whether item is an associative list
|
||||
// element, which should be referenced by key(s), or if it is not and therefore
|
||||
// referencing by index is acceptable. Currently this is done by checking
|
||||
// whether item has any of the fields listed in
|
||||
// AssociativeListCandidateFieldNames which have scalar values.
|
||||
func GuessBestListPathElement(index int, item value.Value) PathElement {
|
||||
if item.MapValue == nil {
|
||||
// Non map items could be parts of sets or regular "atomic"
|
||||
// lists. We won't try to guess whether something should be a
|
||||
// set or not.
|
||||
return PathElement{Index: &index}
|
||||
}
|
||||
|
||||
var keys []value.Field
|
||||
for _, name := range AssociativeListCandidateFieldNames {
|
||||
f, ok := item.MapValue.Get(name)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
// only accept primitive/scalar types as keys.
|
||||
if f.Value.Null || f.Value.MapValue != nil || f.Value.ListValue != nil {
|
||||
continue
|
||||
}
|
||||
keys = append(keys, *f)
|
||||
}
|
||||
if len(keys) > 0 {
|
||||
return PathElement{Key: keys}
|
||||
}
|
||||
return PathElement{Index: &index}
|
||||
}
|
||||
74
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/managers.go
generated
vendored
Normal file
74
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/managers.go
generated
vendored
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
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 fieldpath
|
||||
|
||||
// APIVersion describes the version of an object or of a fieldset.
|
||||
type APIVersion string
|
||||
|
||||
// VersionedSet associates a version to a set.
|
||||
type VersionedSet struct {
|
||||
*Set
|
||||
APIVersion APIVersion
|
||||
Applied bool
|
||||
}
|
||||
|
||||
// ManagedFields is a map from manager to VersionedSet (what they own in
|
||||
// what version).
|
||||
type ManagedFields map[string]*VersionedSet
|
||||
|
||||
// Difference returns a symmetric difference between two Managers. If a
|
||||
// given user's entry has version X in lhs and version Y in rhs, then
|
||||
// the return value for that user will be from rhs. If the difference for
|
||||
// a user is an empty set, that user will not be inserted in the map.
|
||||
func (lhs ManagedFields) Difference(rhs ManagedFields) ManagedFields {
|
||||
diff := ManagedFields{}
|
||||
|
||||
for manager, left := range lhs {
|
||||
right, ok := rhs[manager]
|
||||
if !ok {
|
||||
if !left.Empty() {
|
||||
diff[manager] = left
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// If we have sets in both but their version
|
||||
// differs, we don't even diff and keep the
|
||||
// entire thing.
|
||||
if left.APIVersion != right.APIVersion {
|
||||
diff[manager] = right
|
||||
continue
|
||||
}
|
||||
|
||||
newSet := left.Difference(right.Set).Union(right.Difference(left.Set))
|
||||
if !newSet.Empty() {
|
||||
diff[manager] = &VersionedSet{
|
||||
Set: newSet,
|
||||
APIVersion: right.APIVersion,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for manager, set := range rhs {
|
||||
if _, ok := lhs[manager]; ok {
|
||||
// Already done
|
||||
continue
|
||||
}
|
||||
if !set.Empty() {
|
||||
diff[manager] = set
|
||||
}
|
||||
}
|
||||
|
||||
return diff
|
||||
}
|
||||
81
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/path.go
generated
vendored
Normal file
81
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/path.go
generated
vendored
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 fieldpath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
// Path describes how to select a potentially deeply-nested child field given a
|
||||
// containing object.
|
||||
type Path []PathElement
|
||||
|
||||
func (fp Path) String() string {
|
||||
strs := make([]string, len(fp))
|
||||
for i := range fp {
|
||||
strs[i] = fp[i].String()
|
||||
}
|
||||
return strings.Join(strs, "")
|
||||
}
|
||||
|
||||
func (fp Path) Copy() Path {
|
||||
new := make(Path, len(fp))
|
||||
copy(new, fp)
|
||||
return new
|
||||
}
|
||||
|
||||
// MakePath constructs a Path. The parts may be PathElements, ints, strings.
|
||||
func MakePath(parts ...interface{}) (Path, error) {
|
||||
var fp Path
|
||||
for _, p := range parts {
|
||||
switch t := p.(type) {
|
||||
case PathElement:
|
||||
fp = append(fp, t)
|
||||
case int:
|
||||
// TODO: Understand schema and object and convert this to the
|
||||
// FieldSpecifier below if appropriate.
|
||||
fp = append(fp, PathElement{Index: &t})
|
||||
case string:
|
||||
fp = append(fp, PathElement{FieldName: &t})
|
||||
case []value.Field:
|
||||
if len(t) == 0 {
|
||||
return nil, fmt.Errorf("associative list key type path elements must have at least one key (got zero)")
|
||||
}
|
||||
fp = append(fp, PathElement{Key: t})
|
||||
case value.Value:
|
||||
// TODO: understand schema and verify that this is a set type
|
||||
// TODO: make a copy of t
|
||||
fp = append(fp, PathElement{Value: &t})
|
||||
default:
|
||||
return nil, fmt.Errorf("unable to make %#v into a path element", p)
|
||||
}
|
||||
}
|
||||
return fp, nil
|
||||
}
|
||||
|
||||
// MakePathOrDie panics if parts can't be turned into a path. Good for things
|
||||
// that are known at complie time.
|
||||
func MakePathOrDie(parts ...interface{}) Path {
|
||||
fp, err := MakePath(parts...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return fp
|
||||
}
|
||||
311
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/set.go
generated
vendored
Normal file
311
vendor/sigs.k8s.io/structured-merge-diff/fieldpath/set.go
generated
vendored
Normal file
|
|
@ -0,0 +1,311 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 fieldpath
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Set identifies a set of fields.
|
||||
type Set struct {
|
||||
// Members lists fields that are part of the set.
|
||||
// TODO: will be serialized as a list of path elements.
|
||||
Members PathElementSet
|
||||
|
||||
// Children lists child fields which themselves have children that are
|
||||
// members of the set. Appearance in this list does not imply membership.
|
||||
// Note: this is a tree, not an arbitrary graph.
|
||||
Children SetNodeMap
|
||||
}
|
||||
|
||||
// NewSet makes a set from a list of paths.
|
||||
func NewSet(paths ...Path) *Set {
|
||||
s := &Set{}
|
||||
for _, p := range paths {
|
||||
s.Insert(p)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Insert adds the field identified by `p` to the set. Important: parent fields
|
||||
// are NOT added to the set; if that is desired, they must be added separately.
|
||||
func (s *Set) Insert(p Path) {
|
||||
if len(p) == 0 {
|
||||
// Zero-length path identifies the entire object; we don't
|
||||
// track top-level ownership.
|
||||
return
|
||||
}
|
||||
for {
|
||||
if len(p) == 1 {
|
||||
s.Members.Insert(p[0])
|
||||
return
|
||||
}
|
||||
s = s.Children.Descend(p[0])
|
||||
p = p[1:]
|
||||
}
|
||||
}
|
||||
|
||||
// Union returns a Set containing elements which appear in either s or s2.
|
||||
func (s *Set) Union(s2 *Set) *Set {
|
||||
return &Set{
|
||||
Members: *s.Members.Union(&s2.Members),
|
||||
Children: *s.Children.Union(&s2.Children),
|
||||
}
|
||||
}
|
||||
|
||||
// Intersection returns a Set containing leaf elements which appear in both s
|
||||
// and s2. Intersection can be constructed from Union and Difference operations
|
||||
// (example in the tests) but it's much faster to do it in one pass.
|
||||
func (s *Set) Intersection(s2 *Set) *Set {
|
||||
return &Set{
|
||||
Members: *s.Members.Intersection(&s2.Members),
|
||||
Children: *s.Children.Intersection(&s2.Children),
|
||||
}
|
||||
}
|
||||
|
||||
// Difference returns a Set containing elements which:
|
||||
// * appear in s
|
||||
// * do not appear in s2
|
||||
//
|
||||
// In other words, for leaf fields, this acts like a regular set difference
|
||||
// operation. When non leaf fields are compared with leaf fields ("parents"
|
||||
// which contain "children"), the effect is:
|
||||
// * parent - child = parent
|
||||
// * child - parent = {empty set}
|
||||
func (s *Set) Difference(s2 *Set) *Set {
|
||||
return &Set{
|
||||
Members: *s.Members.Difference(&s2.Members),
|
||||
Children: *s.Children.Difference(s2),
|
||||
}
|
||||
}
|
||||
|
||||
// Size returns the number of members of the set.
|
||||
func (s *Set) Size() int {
|
||||
return s.Members.Size() + s.Children.Size()
|
||||
}
|
||||
|
||||
// Empty returns true if there are no members of the set. It is a separate
|
||||
// function from Size since it's common to check whether size > 0, and
|
||||
// potentially much faster to return as soon as a single element is found.
|
||||
func (s *Set) Empty() bool {
|
||||
if s.Members.Size() > 0 {
|
||||
return false
|
||||
}
|
||||
return s.Children.Empty()
|
||||
}
|
||||
|
||||
// Has returns true if the field referenced by `p` is a member of the set.
|
||||
func (s *Set) Has(p Path) bool {
|
||||
if len(p) == 0 {
|
||||
// No one owns "the entire object"
|
||||
return false
|
||||
}
|
||||
for {
|
||||
if len(p) == 1 {
|
||||
return s.Members.Has(p[0])
|
||||
}
|
||||
var ok bool
|
||||
s, ok = s.Children.Get(p[0])
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
p = p[1:]
|
||||
}
|
||||
}
|
||||
|
||||
// Equals returns true if s and s2 have exactly the same members.
|
||||
func (s *Set) Equals(s2 *Set) bool {
|
||||
return s.Members.Equals(&s2.Members) && s.Children.Equals(&s2.Children)
|
||||
}
|
||||
|
||||
// String returns the set one element per line.
|
||||
func (s *Set) String() string {
|
||||
elements := []string{}
|
||||
s.Iterate(func(p Path) {
|
||||
elements = append(elements, p.String())
|
||||
})
|
||||
return strings.Join(elements, "\n")
|
||||
}
|
||||
|
||||
// Iterate calls f once for each field that is a member of the set (preorder
|
||||
// DFS). The path passed to f will be reused so make a copy if you wish to keep
|
||||
// it.
|
||||
func (s *Set) Iterate(f func(Path)) {
|
||||
s.iteratePrefix(Path{}, f)
|
||||
}
|
||||
|
||||
func (s *Set) iteratePrefix(prefix Path, f func(Path)) {
|
||||
s.Members.Iterate(func(pe PathElement) { f(append(prefix, pe)) })
|
||||
s.Children.iteratePrefix(prefix, f)
|
||||
}
|
||||
|
||||
// WithPrefix returns the subset of paths which begin with the given prefix,
|
||||
// with the prefix not included.
|
||||
func (s *Set) WithPrefix(pe PathElement) *Set {
|
||||
subset, ok := s.Children.Get(pe)
|
||||
if !ok {
|
||||
return NewSet()
|
||||
}
|
||||
return subset
|
||||
}
|
||||
|
||||
// setNode is a pair of PathElement / Set, for the purpose of expressing
|
||||
// nested set membership.
|
||||
type setNode struct {
|
||||
pathElement PathElement
|
||||
set *Set
|
||||
}
|
||||
|
||||
// SetNodeMap is a map of PathElement to subset.
|
||||
type SetNodeMap struct {
|
||||
members map[string]setNode
|
||||
}
|
||||
|
||||
// Descend adds pe to the set if necessary, returning the associated subset.
|
||||
func (s *SetNodeMap) Descend(pe PathElement) *Set {
|
||||
serialized := pe.String()
|
||||
if s.members == nil {
|
||||
s.members = map[string]setNode{}
|
||||
}
|
||||
if n, ok := s.members[serialized]; ok {
|
||||
return n.set
|
||||
}
|
||||
ss := &Set{}
|
||||
s.members[serialized] = setNode{
|
||||
pathElement: pe,
|
||||
set: ss,
|
||||
}
|
||||
return ss
|
||||
}
|
||||
|
||||
// Size returns the sum of the number of members of all subsets.
|
||||
func (s *SetNodeMap) Size() int {
|
||||
count := 0
|
||||
for _, v := range s.members {
|
||||
count += v.set.Size()
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// Empty returns false if there's at least one member in some child set.
|
||||
func (s *SetNodeMap) Empty() bool {
|
||||
for _, n := range s.members {
|
||||
if !n.set.Empty() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Get returns (the associated set, true) or (nil, false) if there is none.
|
||||
func (s *SetNodeMap) Get(pe PathElement) (*Set, bool) {
|
||||
if s.members == nil {
|
||||
return nil, false
|
||||
}
|
||||
serialized := pe.String()
|
||||
if n, ok := s.members[serialized]; ok {
|
||||
return n.set, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Equals returns true if s and s2 have the same structure (same nested
|
||||
// child sets).
|
||||
func (s *SetNodeMap) Equals(s2 *SetNodeMap) bool {
|
||||
if len(s.members) != len(s2.members) {
|
||||
return false
|
||||
}
|
||||
for k, v := range s.members {
|
||||
v2, ok := s2.members[k]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if !v.set.Equals(v2.set) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Union returns a SetNodeMap with members that appear in either s or s2.
|
||||
func (s *SetNodeMap) Union(s2 *SetNodeMap) *SetNodeMap {
|
||||
out := &SetNodeMap{}
|
||||
for k, sn := range s.members {
|
||||
pe := sn.pathElement
|
||||
if sn2, ok := s2.members[k]; ok {
|
||||
*out.Descend(pe) = *sn.set.Union(sn2.set)
|
||||
} else {
|
||||
*out.Descend(pe) = *sn.set
|
||||
}
|
||||
}
|
||||
for k, sn2 := range s2.members {
|
||||
pe := sn2.pathElement
|
||||
if _, ok := s.members[k]; ok {
|
||||
// already handled
|
||||
continue
|
||||
}
|
||||
*out.Descend(pe) = *sn2.set
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Intersection returns a SetNodeMap with members that appear in both s and s2.
|
||||
func (s *SetNodeMap) Intersection(s2 *SetNodeMap) *SetNodeMap {
|
||||
out := &SetNodeMap{}
|
||||
for k, sn := range s.members {
|
||||
pe := sn.pathElement
|
||||
if sn2, ok := s2.members[k]; ok {
|
||||
i := *sn.set.Intersection(sn2.set)
|
||||
if !i.Empty() {
|
||||
*out.Descend(pe) = i
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Difference returns a SetNodeMap with members that appear in s but not in s2.
|
||||
func (s *SetNodeMap) Difference(s2 *Set) *SetNodeMap {
|
||||
out := &SetNodeMap{}
|
||||
for k, sn := range s.members {
|
||||
pe := sn.pathElement
|
||||
if sn2, ok := s2.Children.members[k]; ok {
|
||||
diff := *sn.set.Difference(sn2.set)
|
||||
// We aren't permitted to add nodes with no elements.
|
||||
if !diff.Empty() {
|
||||
*out.Descend(pe) = diff
|
||||
}
|
||||
} else {
|
||||
*out.Descend(pe) = *sn.set
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Iterate calls f for each PathElement in the set.
|
||||
func (s *SetNodeMap) Iterate(f func(PathElement)) {
|
||||
for _, n := range s.members {
|
||||
f(n.pathElement)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SetNodeMap) iteratePrefix(prefix Path, f func(Path)) {
|
||||
for _, n := range s.members {
|
||||
pe := n.pathElement
|
||||
n.set.iteratePrefix(append(prefix, pe), f)
|
||||
}
|
||||
}
|
||||
91
vendor/sigs.k8s.io/structured-merge-diff/merge/conflict.go
generated
vendored
Normal file
91
vendor/sigs.k8s.io/structured-merge-diff/merge/conflict.go
generated
vendored
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 merge
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
)
|
||||
|
||||
// Conflict is a conflict on a specific field with the current manager of
|
||||
// that field. It does implement the error interface so that it can be
|
||||
// used as an error.
|
||||
type Conflict struct {
|
||||
Manager string
|
||||
Path fieldpath.Path
|
||||
}
|
||||
|
||||
// Conflict is an error.
|
||||
var _ error = Conflict{}
|
||||
|
||||
// Error formats the conflict as an error.
|
||||
func (c Conflict) Error() string {
|
||||
return fmt.Sprintf("conflict with %q: %v", c.Manager, c.Path)
|
||||
}
|
||||
|
||||
// Conflicts accumulates multiple conflicts and aggregates them by managers.
|
||||
type Conflicts []Conflict
|
||||
|
||||
var _ error = Conflicts{}
|
||||
|
||||
// Error prints the list of conflicts, grouped by sorted managers.
|
||||
func (conflicts Conflicts) Error() string {
|
||||
if len(conflicts) == 1 {
|
||||
return conflicts[0].Error()
|
||||
}
|
||||
|
||||
m := map[string][]fieldpath.Path{}
|
||||
for _, conflict := range conflicts {
|
||||
m[conflict.Manager] = append(m[conflict.Manager], conflict.Path)
|
||||
}
|
||||
|
||||
managers := []string{}
|
||||
for manager := range m {
|
||||
managers = append(managers, manager)
|
||||
}
|
||||
|
||||
// Print conflicts by sorted managers.
|
||||
sort.Strings(managers)
|
||||
|
||||
messages := []string{}
|
||||
for _, manager := range managers {
|
||||
messages = append(messages, fmt.Sprintf("conflicts with %q:", manager))
|
||||
for _, path := range m[manager] {
|
||||
messages = append(messages, fmt.Sprintf("- %v", path))
|
||||
}
|
||||
}
|
||||
return strings.Join(messages, "\n")
|
||||
}
|
||||
|
||||
// ConflictsFromManagers creates a list of conflicts given Managers sets.
|
||||
func ConflictsFromManagers(sets fieldpath.ManagedFields) Conflicts {
|
||||
conflicts := []Conflict{}
|
||||
|
||||
for manager, set := range sets {
|
||||
set.Iterate(func(p fieldpath.Path) {
|
||||
conflicts = append(conflicts, Conflict{
|
||||
Manager: manager,
|
||||
Path: p,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return conflicts
|
||||
}
|
||||
273
vendor/sigs.k8s.io/structured-merge-diff/merge/update.go
generated
vendored
Normal file
273
vendor/sigs.k8s.io/structured-merge-diff/merge/update.go
generated
vendored
Normal file
|
|
@ -0,0 +1,273 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
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 merge
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/typed"
|
||||
)
|
||||
|
||||
// Converter is an interface to the conversion logic. The converter
|
||||
// needs to be able to convert objects from one version to another.
|
||||
type Converter interface {
|
||||
Convert(object typed.TypedValue, version fieldpath.APIVersion) (typed.TypedValue, error)
|
||||
IsMissingVersionError(error) bool
|
||||
}
|
||||
|
||||
// Updater is the object used to compute updated FieldSets and also
|
||||
// merge the object on Apply.
|
||||
type Updater struct {
|
||||
Converter Converter
|
||||
}
|
||||
|
||||
func (s *Updater) update(oldObject, newObject typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, workflow string, force bool) (fieldpath.ManagedFields, error) {
|
||||
conflicts := fieldpath.ManagedFields{}
|
||||
removed := fieldpath.ManagedFields{}
|
||||
type Versioned struct {
|
||||
oldObject typed.TypedValue
|
||||
newObject typed.TypedValue
|
||||
}
|
||||
versions := map[fieldpath.APIVersion]Versioned{
|
||||
version: Versioned{
|
||||
oldObject: oldObject,
|
||||
newObject: newObject,
|
||||
},
|
||||
}
|
||||
|
||||
for manager, managerSet := range managers {
|
||||
if manager == workflow {
|
||||
continue
|
||||
}
|
||||
versioned, ok := versions[managerSet.APIVersion]
|
||||
if !ok {
|
||||
var err error
|
||||
versioned.oldObject, err = s.Converter.Convert(oldObject, managerSet.APIVersion)
|
||||
if err != nil {
|
||||
if s.Converter.IsMissingVersionError(err) {
|
||||
delete(managers, manager)
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("failed to convert old object: %v", err)
|
||||
}
|
||||
versioned.newObject, err = s.Converter.Convert(newObject, managerSet.APIVersion)
|
||||
if err != nil {
|
||||
if s.Converter.IsMissingVersionError(err) {
|
||||
delete(managers, manager)
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("failed to convert new object: %v", err)
|
||||
}
|
||||
versions[managerSet.APIVersion] = versioned
|
||||
}
|
||||
compare, err := versioned.oldObject.Compare(versioned.newObject)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to compare objects: %v", err)
|
||||
}
|
||||
|
||||
conflictSet := managerSet.Intersection(compare.Modified.Union(compare.Added))
|
||||
if !conflictSet.Empty() {
|
||||
conflicts[manager] = &fieldpath.VersionedSet{
|
||||
Set: conflictSet,
|
||||
APIVersion: managerSet.APIVersion,
|
||||
}
|
||||
}
|
||||
|
||||
if !compare.Removed.Empty() {
|
||||
removed[manager] = &fieldpath.VersionedSet{
|
||||
Set: compare.Removed,
|
||||
APIVersion: managerSet.APIVersion,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !force && len(conflicts) != 0 {
|
||||
return nil, ConflictsFromManagers(conflicts)
|
||||
}
|
||||
|
||||
for manager, conflictSet := range conflicts {
|
||||
managers[manager].Set = managers[manager].Set.Difference(conflictSet.Set)
|
||||
}
|
||||
|
||||
for manager, removedSet := range removed {
|
||||
managers[manager].Set = managers[manager].Set.Difference(removedSet.Set)
|
||||
}
|
||||
|
||||
for manager := range managers {
|
||||
if managers[manager].Set.Empty() {
|
||||
delete(managers, manager)
|
||||
}
|
||||
}
|
||||
|
||||
return managers, nil
|
||||
}
|
||||
|
||||
// Update is the method you should call once you've merged your final
|
||||
// object on CREATE/UPDATE/PATCH verbs. newObject must be the object
|
||||
// that you intend to persist (after applying the patch if this is for a
|
||||
// PATCH call), and liveObject must be the original object (empty if
|
||||
// this is a CREATE call).
|
||||
func (s *Updater) Update(liveObject, newObject typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string) (fieldpath.ManagedFields, error) {
|
||||
var err error
|
||||
managers = shallowCopyManagers(managers)
|
||||
managers, err = s.update(liveObject, newObject, version, managers, manager, true)
|
||||
if err != nil {
|
||||
return fieldpath.ManagedFields{}, err
|
||||
}
|
||||
compare, err := liveObject.Compare(newObject)
|
||||
if err != nil {
|
||||
return fieldpath.ManagedFields{}, fmt.Errorf("failed to compare live and new objects: %v", err)
|
||||
}
|
||||
if _, ok := managers[manager]; !ok {
|
||||
managers[manager] = &fieldpath.VersionedSet{
|
||||
Set: fieldpath.NewSet(),
|
||||
}
|
||||
}
|
||||
managers[manager].Set = managers[manager].Set.Union(compare.Modified).Union(compare.Added).Difference(compare.Removed)
|
||||
managers[manager].APIVersion = version
|
||||
if managers[manager].Set.Empty() {
|
||||
delete(managers, manager)
|
||||
}
|
||||
return managers, nil
|
||||
}
|
||||
|
||||
// Apply should be called when Apply is run, given the current object as
|
||||
// well as the configuration that is applied. This will merge the object
|
||||
// and return it.
|
||||
func (s *Updater) Apply(liveObject, configObject typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string, force bool) (typed.TypedValue, fieldpath.ManagedFields, error) {
|
||||
managers = shallowCopyManagers(managers)
|
||||
newObject, err := liveObject.Merge(configObject)
|
||||
if err != nil {
|
||||
return nil, fieldpath.ManagedFields{}, fmt.Errorf("failed to merge config: %v", err)
|
||||
}
|
||||
lastSet := managers[manager]
|
||||
set, err := configObject.ToFieldSet()
|
||||
if err != nil {
|
||||
return nil, fieldpath.ManagedFields{}, fmt.Errorf("failed to get field set: %v", err)
|
||||
}
|
||||
managers[manager] = &fieldpath.VersionedSet{
|
||||
Set: set,
|
||||
APIVersion: version,
|
||||
Applied: true,
|
||||
}
|
||||
newObject, err = s.prune(newObject, managers, manager, lastSet)
|
||||
if err != nil {
|
||||
return nil, fieldpath.ManagedFields{}, fmt.Errorf("failed to prune fields: %v", err)
|
||||
}
|
||||
managers, err = s.update(liveObject, newObject, version, managers, manager, force)
|
||||
if err != nil {
|
||||
return nil, fieldpath.ManagedFields{}, err
|
||||
}
|
||||
return newObject, managers, nil
|
||||
}
|
||||
|
||||
func shallowCopyManagers(managers fieldpath.ManagedFields) fieldpath.ManagedFields {
|
||||
newManagers := fieldpath.ManagedFields{}
|
||||
for manager, set := range managers {
|
||||
newManagers[manager] = set
|
||||
}
|
||||
return newManagers
|
||||
}
|
||||
|
||||
// prune will remove a list or map item, iff:
|
||||
// * applyingManager applied it last time
|
||||
// * applyingManager didn't apply it this time
|
||||
// * no other applier claims to manage it
|
||||
func (s *Updater) prune(merged typed.TypedValue, managers fieldpath.ManagedFields, applyingManager string, lastSet *fieldpath.VersionedSet) (typed.TypedValue, error) {
|
||||
if lastSet == nil || lastSet.Set.Empty() {
|
||||
return merged, nil
|
||||
}
|
||||
convertedMerged, err := s.Converter.Convert(merged, lastSet.APIVersion)
|
||||
if err != nil {
|
||||
if s.Converter.IsMissingVersionError(err) {
|
||||
return merged, nil
|
||||
}
|
||||
return nil, fmt.Errorf("failed to convert merged object to last applied version: %v", err)
|
||||
}
|
||||
pruned := convertedMerged.RemoveItems(lastSet.Set)
|
||||
pruned, err = s.addBackOwnedItems(convertedMerged, pruned, managers, applyingManager)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed add back owned items: %v", err)
|
||||
}
|
||||
pruned, err = s.addBackDanglingItems(convertedMerged, pruned, lastSet)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed add back dangling items: %v", err)
|
||||
}
|
||||
return s.Converter.Convert(pruned, managers[applyingManager].APIVersion)
|
||||
}
|
||||
|
||||
// addBackOwnedItems adds back any list and map items that were removed by prune,
|
||||
// but other appliers (or the current applier's new config) claim to own.
|
||||
func (s *Updater) addBackOwnedItems(merged, pruned typed.TypedValue, managedFields fieldpath.ManagedFields, applyingManager string) (typed.TypedValue, error) {
|
||||
var err error
|
||||
managedAtVersion := map[fieldpath.APIVersion]*fieldpath.Set{}
|
||||
for _, managerSet := range managedFields {
|
||||
if managerSet.Applied {
|
||||
if _, ok := managedAtVersion[managerSet.APIVersion]; !ok {
|
||||
managedAtVersion[managerSet.APIVersion] = fieldpath.NewSet()
|
||||
}
|
||||
managedAtVersion[managerSet.APIVersion] = managedAtVersion[managerSet.APIVersion].Union(managerSet.Set)
|
||||
}
|
||||
}
|
||||
for version, managed := range managedAtVersion {
|
||||
merged, err = s.Converter.Convert(merged, version)
|
||||
if err != nil {
|
||||
if s.Converter.IsMissingVersionError(err) {
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("failed to convert merged object at version %v: %v", version, err)
|
||||
}
|
||||
pruned, err = s.Converter.Convert(pruned, version)
|
||||
if err != nil {
|
||||
if s.Converter.IsMissingVersionError(err) {
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("failed to convert pruned object at version %v: %v", version, err)
|
||||
}
|
||||
mergedSet, err := merged.ToFieldSet()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create field set from merged object at version %v: %v", version, err)
|
||||
}
|
||||
prunedSet, err := pruned.ToFieldSet()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create field set from pruned object at version %v: %v", version, err)
|
||||
}
|
||||
pruned = merged.RemoveItems(mergedSet.Difference(prunedSet.Union(managed)))
|
||||
}
|
||||
return pruned, nil
|
||||
}
|
||||
|
||||
|
||||
// addBackDanglingItems makes sure that the only items removed by prune are items that were
|
||||
// previously owned by the currently applying manager. This will add back unowned items and items
|
||||
// which are owned by Updaters that shouldn't be removed.
|
||||
func (s *Updater) addBackDanglingItems(merged, pruned typed.TypedValue, lastSet *fieldpath.VersionedSet) (typed.TypedValue, error) {
|
||||
convertedPruned, err := s.Converter.Convert(pruned, lastSet.APIVersion)
|
||||
if err != nil {
|
||||
if s.Converter.IsMissingVersionError(err) {
|
||||
return merged, nil
|
||||
}
|
||||
return nil, fmt.Errorf("failed to convert pruned object to last applied version: %v", err)
|
||||
}
|
||||
prunedSet, err := convertedPruned.ToFieldSet()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create field set from pruned object in last applied version: %v", err)
|
||||
}
|
||||
mergedSet, err := merged.ToFieldSet()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create field set from merged object in last applied version: %v", err)
|
||||
}
|
||||
return merged.RemoveItems(mergedSet.Difference(prunedSet).Intersection(lastSet.Set)), nil
|
||||
}
|
||||
28
vendor/sigs.k8s.io/structured-merge-diff/schema/doc.go
generated
vendored
Normal file
28
vendor/sigs.k8s.io/structured-merge-diff/schema/doc.go
generated
vendored
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 schema defines a targeted schema language which allows one to
|
||||
// represent all the schema information necessary to perform "structured"
|
||||
// merges and diffs.
|
||||
//
|
||||
// Due to the targeted nature of the data model, the schema language can fit in
|
||||
// just a few hundred lines of go code, making it much more understandable and
|
||||
// concise than e.g. OpenAPI.
|
||||
//
|
||||
// This schema was derived by observing the API objects used by Kubernetes, and
|
||||
// formalizing a model which allows certain operations ("apply") to be more
|
||||
// well defined. It is currently missing one feature: one-of ("unions").
|
||||
package schema
|
||||
219
vendor/sigs.k8s.io/structured-merge-diff/schema/elements.go
generated
vendored
Normal file
219
vendor/sigs.k8s.io/structured-merge-diff/schema/elements.go
generated
vendored
Normal file
|
|
@ -0,0 +1,219 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 schema
|
||||
|
||||
// Schema is a list of named types.
|
||||
type Schema struct {
|
||||
Types []TypeDef `yaml:"types,omitempty"`
|
||||
}
|
||||
|
||||
// A TypeSpecifier references a particular type in a schema.
|
||||
type TypeSpecifier struct {
|
||||
Type TypeRef `yaml:"type,omitempty"`
|
||||
Schema Schema `yaml:"schema,omitempty"`
|
||||
}
|
||||
|
||||
// TypeDef represents a named type in a schema.
|
||||
type TypeDef struct {
|
||||
// Top level types should be named. Every type must have a unique name.
|
||||
Name string `yaml:"name,omitempty"`
|
||||
|
||||
Atom `yaml:"atom,omitempty,inline"`
|
||||
}
|
||||
|
||||
// TypeRef either refers to a named type or declares an inlined type.
|
||||
type TypeRef struct {
|
||||
// Either the name or one member of Atom should be set.
|
||||
NamedType *string `yaml:"namedType,omitempty"`
|
||||
Inlined Atom `yaml:",inline,omitempty"`
|
||||
}
|
||||
|
||||
// Atom represents the smallest possible pieces of the type system.
|
||||
type Atom struct {
|
||||
// Exactly one of the below must be set.
|
||||
*Scalar `yaml:"scalar,omitempty"`
|
||||
*Struct `yaml:"struct,omitempty"`
|
||||
*List `yaml:"list,omitempty"`
|
||||
*Map `yaml:"map,omitempty"`
|
||||
*Untyped `yaml:"untyped,omitempty"`
|
||||
}
|
||||
|
||||
// Scalar (AKA "primitive") represents a type which has a single value which is
|
||||
// either numeric, string, or boolean.
|
||||
//
|
||||
// TODO: split numeric into float/int? Something even more fine-grained?
|
||||
type Scalar string
|
||||
|
||||
const (
|
||||
Numeric = Scalar("numeric")
|
||||
String = Scalar("string")
|
||||
Boolean = Scalar("boolean")
|
||||
)
|
||||
|
||||
// ElementRelationship is an enum of the different possible relationships
|
||||
// between the elements of container types (maps, lists, structs, untyped).
|
||||
type ElementRelationship string
|
||||
|
||||
const (
|
||||
// Associative only applies to lists (see the documentation there).
|
||||
Associative = ElementRelationship("associative")
|
||||
// Atomic makes container types (lists, maps, structs, untyped) behave
|
||||
// as scalars / leaf fields (which is the default for untyped data).
|
||||
Atomic = ElementRelationship("atomic")
|
||||
// Separable means the items of the container type have no particular
|
||||
// relationship (default behavior for maps and structs).
|
||||
Separable = ElementRelationship("separable")
|
||||
)
|
||||
|
||||
// Struct represents a type which is composed of a number of different fields.
|
||||
// Each field has a name and a type.
|
||||
//
|
||||
// TODO: in the future, we will add one-of groups (sometimes called unions).
|
||||
type Struct struct {
|
||||
// Each struct field appears exactly once in this list. The order in
|
||||
// this list defines the canonical field ordering.
|
||||
Fields []StructField `yaml:"fields,omitempty"`
|
||||
|
||||
// TODO: Implement unions, either this way or by inlining.
|
||||
// Unions are groupings of fields with special rules. They may refer to
|
||||
// one or more fields in the above list. A given field from the above
|
||||
// list may be referenced in exactly 0 or 1 places in the below list.
|
||||
// Unions []Union `yaml:"unions,omitempty"`
|
||||
|
||||
// ElementRelationship states the relationship between the struct's items.
|
||||
// * `separable` (or unset) implies that each element is 100% independent.
|
||||
// * `atomic` implies that all elements depend on each other, and this
|
||||
// is effectively a scalar / leaf field; it doesn't make sense for
|
||||
// separate actors to set the elements. Example: an RGB color struct;
|
||||
// it would never make sense to "own" only one component of the
|
||||
// color.
|
||||
// The default behavior for structs is `separable`; it's permitted to
|
||||
// leave this unset to get the default behavior.
|
||||
ElementRelationship ElementRelationship `yaml:"elementRelationship,omitempty"`
|
||||
}
|
||||
|
||||
// StructField pairs a field name with a field type.
|
||||
type StructField struct {
|
||||
// Name is the field name.
|
||||
Name string `yaml:"name,omitempty"`
|
||||
// Type is the field type.
|
||||
Type TypeRef `yaml:"type,omitempty"`
|
||||
}
|
||||
|
||||
// List represents a type which contains a zero or more elements, all of the
|
||||
// same subtype. Lists may be either associative: each element is more or less
|
||||
// independent and could be managed by separate entities in the system; or
|
||||
// atomic, where the elements are heavily dependent on each other: it is not
|
||||
// sensible to change one element without considering the ramifications on all
|
||||
// the other elements.
|
||||
type List struct {
|
||||
// ElementType is the type of the list's elements.
|
||||
ElementType TypeRef `yaml:"elementType,omitempty"`
|
||||
|
||||
// ElementRelationship states the relationship between the list's elements
|
||||
// and must have one of these values:
|
||||
// * `atomic`: the list is treated as a single entity, like a scalar.
|
||||
// * `associative`:
|
||||
// - If the list element is a scalar, the list is treated as a set.
|
||||
// - If the list element is a struct, the list is treated as a map.
|
||||
// - The list element must not be a map or a list itself.
|
||||
// There is no default for this value for lists; all schemas must
|
||||
// explicitly state the element relationship for all lists.
|
||||
ElementRelationship ElementRelationship `yaml:"elementRelationship,omitempty"`
|
||||
|
||||
// Iff ElementRelationship is `associative`, and the element type is
|
||||
// struct, then Keys must have non-zero length, and it lists the fields
|
||||
// of the element's struct type which are to be used as the keys of the
|
||||
// list.
|
||||
//
|
||||
// TODO: change this to "non-atomic struct" above and make the code reflect this.
|
||||
//
|
||||
// Each key must refer to a single field name (no nesting, not JSONPath).
|
||||
Keys []string `yaml:"keys,omitempty"`
|
||||
}
|
||||
|
||||
// Map is a key-value pair. Its default semantics are the same as an
|
||||
// associative list, but:
|
||||
// * It is serialized differently:
|
||||
// map: {"k": {"value": "v"}}
|
||||
// list: [{"key": "k", "value": "v"}]
|
||||
// * Keys must be string typed.
|
||||
// * Keys can't have multiple components.
|
||||
//
|
||||
// Although serialized the same, maps are different from structs in that each
|
||||
// map item must have the same type.
|
||||
//
|
||||
// Optionally, maps may be atomic (for example, imagine representing an RGB
|
||||
// color value--it doesn't make sense to have different actors own the R and G
|
||||
// values).
|
||||
type Map struct {
|
||||
// ElementType is the type of the list's elements.
|
||||
ElementType TypeRef `yaml:"elementType,omitempty"`
|
||||
|
||||
// ElementRelationship states the relationship between the map's items.
|
||||
// * `separable` implies that each element is 100% independent.
|
||||
// * `atomic` implies that all elements depend on each other, and this
|
||||
// is effectively a scalar / leaf field; it doesn't make sense for
|
||||
// separate actors to set the elements.
|
||||
// TODO: find a simple example.
|
||||
// The default behavior for maps is `separable`; it's permitted to
|
||||
// leave this unset to get the default behavior.
|
||||
ElementRelationship ElementRelationship `yaml:"elementRelationship,omitempty"`
|
||||
}
|
||||
|
||||
// Untyped represents types that allow arbitrary content. (Think: plugin
|
||||
// objects.)
|
||||
type Untyped struct {
|
||||
// ElementRelationship states the relationship between the items, if
|
||||
// container-typed data happens to be present here.
|
||||
// * `atomic` implies that all elements depend on each other, and this
|
||||
// is effectively a scalar / leaf field; it doesn't make sense for
|
||||
// separate actors to set the elements.
|
||||
// TODO: support "guess" (guesses at associative list keys)
|
||||
// TODO: support "lookup" (calls a lookup function to figure out the
|
||||
// schema based on the data)
|
||||
// The default behavior for untyped data is `atomic`; it's permitted to
|
||||
// leave this unset to get the default behavior.
|
||||
ElementRelationship ElementRelationship `yaml:"elementRelationship,omitempty"`
|
||||
}
|
||||
|
||||
// FindNamedType is a convenience function that returns the referenced TypeDef,
|
||||
// if it exists, or (nil, false) if it doesn't.
|
||||
func (s Schema) FindNamedType(name string) (TypeDef, bool) {
|
||||
for _, t := range s.Types {
|
||||
if t.Name == name {
|
||||
return t, true
|
||||
}
|
||||
}
|
||||
return TypeDef{}, false
|
||||
}
|
||||
|
||||
// Resolve is a convenience function which returns the atom referenced, whether
|
||||
// it is inline or named. Returns (Atom{}, false) if the type can't be resolved.
|
||||
//
|
||||
// This allows callers to not care about the difference between a (possibly
|
||||
// inlined) reference and a definition.
|
||||
func (s *Schema) Resolve(tr TypeRef) (Atom, bool) {
|
||||
if tr.NamedType != nil {
|
||||
t, ok := s.FindNamedType(*tr.NamedType)
|
||||
if !ok {
|
||||
return Atom{}, false
|
||||
}
|
||||
return t.Atom, true
|
||||
}
|
||||
return tr.Inlined, true
|
||||
}
|
||||
57
vendor/sigs.k8s.io/structured-merge-diff/schema/fromvalue.go
generated
vendored
Normal file
57
vendor/sigs.k8s.io/structured-merge-diff/schema/fromvalue.go
generated
vendored
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
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 schema
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
// TypeRefFromValue creates an inlined type from a value v
|
||||
func TypeRefFromValue(v value.Value) TypeRef {
|
||||
atom := atomFor(v)
|
||||
return TypeRef{
|
||||
Inlined: atom,
|
||||
}
|
||||
}
|
||||
|
||||
func atomFor(v value.Value) Atom {
|
||||
switch {
|
||||
// Untyped cases (handled at the bottom of this function)
|
||||
case v.Null:
|
||||
case v.ListValue != nil:
|
||||
case v.FloatValue != nil:
|
||||
case v.IntValue != nil:
|
||||
case v.StringValue != nil:
|
||||
case v.BooleanValue != nil:
|
||||
// Recursive case
|
||||
case v.MapValue != nil:
|
||||
s := Struct{}
|
||||
for i := range v.MapValue.Items {
|
||||
child := v.MapValue.Items[i]
|
||||
field := StructField{
|
||||
Name: child.Name,
|
||||
Type: TypeRef{
|
||||
Inlined: atomFor(child.Value),
|
||||
},
|
||||
}
|
||||
s.Fields = append(s.Fields, field)
|
||||
}
|
||||
return Atom{Struct: &s}
|
||||
}
|
||||
|
||||
return Atom{Untyped: &Untyped{}}
|
||||
}
|
||||
128
vendor/sigs.k8s.io/structured-merge-diff/schema/schemaschema.go
generated
vendored
Normal file
128
vendor/sigs.k8s.io/structured-merge-diff/schema/schemaschema.go
generated
vendored
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 schema
|
||||
|
||||
// SchemaSchemaYAML is a schema against which you can validate other schemas.
|
||||
// It will validate itself. It can be unmarshalled into a Schema type.
|
||||
var SchemaSchemaYAML = `types:
|
||||
- name: schema
|
||||
struct:
|
||||
fields:
|
||||
- name: types
|
||||
type:
|
||||
list:
|
||||
elementRelationship: associative
|
||||
elementType:
|
||||
namedType: typeDef
|
||||
keys:
|
||||
- name
|
||||
- name: typeDef
|
||||
struct:
|
||||
fields:
|
||||
- name: name
|
||||
type:
|
||||
scalar: string
|
||||
- name: scalar
|
||||
type:
|
||||
scalar: string
|
||||
- name: struct
|
||||
type:
|
||||
namedType: struct
|
||||
- name: list
|
||||
type:
|
||||
namedType: list
|
||||
- name: map
|
||||
type:
|
||||
namedType: map
|
||||
- name: untyped
|
||||
type:
|
||||
namedType: untyped
|
||||
- name: typeRef
|
||||
struct:
|
||||
fields:
|
||||
- name: namedType
|
||||
type:
|
||||
scalar: string
|
||||
- name: scalar
|
||||
type:
|
||||
scalar: string
|
||||
- name: struct
|
||||
type:
|
||||
namedType: struct
|
||||
- name: list
|
||||
type:
|
||||
namedType: list
|
||||
- name: map
|
||||
type:
|
||||
namedType: map
|
||||
- name: untyped
|
||||
type:
|
||||
namedType: untyped
|
||||
- name: scalar
|
||||
scalar: string
|
||||
- name: struct
|
||||
struct:
|
||||
fields:
|
||||
- name: fields
|
||||
type:
|
||||
list:
|
||||
elementType:
|
||||
namedType: structField
|
||||
elementRelationship: associative
|
||||
keys: [ "name" ]
|
||||
- name: elementRelationship
|
||||
type:
|
||||
scalar: string
|
||||
- name: structField
|
||||
struct:
|
||||
fields:
|
||||
- name: name
|
||||
type:
|
||||
scalar: string
|
||||
- name: type
|
||||
type:
|
||||
namedType: typeRef
|
||||
- name: list
|
||||
struct:
|
||||
fields:
|
||||
- name: elementType
|
||||
type:
|
||||
namedType: typeRef
|
||||
- name: elementRelationship
|
||||
type:
|
||||
scalar: string
|
||||
- name: keys
|
||||
type:
|
||||
list:
|
||||
elementType:
|
||||
scalar: string
|
||||
- name: map
|
||||
struct:
|
||||
fields:
|
||||
- name: elementType
|
||||
type:
|
||||
namedType: typeRef
|
||||
- name: elementRelationship
|
||||
type:
|
||||
scalar: string
|
||||
- name: untyped
|
||||
struct:
|
||||
fields:
|
||||
- name: elementRelationship
|
||||
type:
|
||||
scalar: string
|
||||
`
|
||||
178
vendor/sigs.k8s.io/structured-merge-diff/typed/deduced.go
generated
vendored
Normal file
178
vendor/sigs.k8s.io/structured-merge-diff/typed/deduced.go
generated
vendored
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 typed
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
// deducedTypedValue holds a value and guesses what it is and what to
|
||||
// do with it.
|
||||
type deducedTypedValue struct {
|
||||
value value.Value
|
||||
}
|
||||
|
||||
// AsTypedDeduced is going to generate it's own type definition based on
|
||||
// the content of the object. This is useful for CRDs that don't have a
|
||||
// validation field.
|
||||
func AsTypedDeduced(v value.Value) TypedValue {
|
||||
return deducedTypedValue{value: v}
|
||||
}
|
||||
|
||||
func (dv deducedTypedValue) AsValue() *value.Value {
|
||||
return &dv.value
|
||||
}
|
||||
|
||||
func (deducedTypedValue) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dv deducedTypedValue) ToFieldSet() (*fieldpath.Set, error) {
|
||||
set := fieldpath.NewSet()
|
||||
fieldsetDeduced(dv.value, fieldpath.Path{}, set)
|
||||
return set, nil
|
||||
}
|
||||
|
||||
func fieldsetDeduced(v value.Value, path fieldpath.Path, set *fieldpath.Set) {
|
||||
if v.MapValue == nil {
|
||||
set.Insert(path)
|
||||
return
|
||||
}
|
||||
|
||||
// We have a map.
|
||||
// copy the existing path, append each item, and recursively call.
|
||||
for i := range v.MapValue.Items {
|
||||
child := v.MapValue.Items[i]
|
||||
np := path.Copy()
|
||||
np = append(np, fieldpath.PathElement{FieldName: &child.Name})
|
||||
fieldsetDeduced(child.Value, np, set)
|
||||
}
|
||||
}
|
||||
|
||||
func (dv deducedTypedValue) Merge(pso TypedValue) (TypedValue, error) {
|
||||
tpso, ok := pso.(deducedTypedValue)
|
||||
if !ok {
|
||||
return nil, errorFormatter{}.
|
||||
errorf("can't merge deducedTypedValue with %T", tpso)
|
||||
}
|
||||
return AsTypedDeduced(mergeDeduced(dv.value, tpso.value)), nil
|
||||
}
|
||||
|
||||
func mergeDeduced(lhs, rhs value.Value) value.Value {
|
||||
// If both sides are maps, merge them, otherwise return right
|
||||
// side.
|
||||
if rhs.MapValue == nil || lhs.MapValue == nil {
|
||||
return rhs
|
||||
}
|
||||
|
||||
v := value.Value{MapValue: &value.Map{}}
|
||||
for i := range lhs.MapValue.Items {
|
||||
child := lhs.MapValue.Items[i]
|
||||
v.MapValue.Set(child.Name, child.Value)
|
||||
}
|
||||
for i := range rhs.MapValue.Items {
|
||||
child := rhs.MapValue.Items[i]
|
||||
if sub, ok := v.MapValue.Get(child.Name); ok {
|
||||
new := mergeDeduced(sub.Value, child.Value)
|
||||
v.MapValue.Set(child.Name, new)
|
||||
} else {
|
||||
v.MapValue.Set(child.Name, child.Value)
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (dv deducedTypedValue) Compare(rhs TypedValue) (c *Comparison, err error) {
|
||||
trhs, ok := rhs.(deducedTypedValue)
|
||||
if !ok {
|
||||
return nil, errorFormatter{}.
|
||||
errorf("can't merge deducedTypedValue with %T", rhs)
|
||||
}
|
||||
|
||||
c = &Comparison{
|
||||
Removed: fieldpath.NewSet(),
|
||||
Modified: fieldpath.NewSet(),
|
||||
Added: fieldpath.NewSet(),
|
||||
}
|
||||
|
||||
added(dv.value, trhs.value, fieldpath.Path{}, c.Added)
|
||||
added(trhs.value, dv.value, fieldpath.Path{}, c.Removed)
|
||||
modified(dv.value, trhs.value, fieldpath.Path{}, c.Modified)
|
||||
|
||||
merge, err := dv.Merge(rhs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Merged = merge
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func added(lhs, rhs value.Value, path fieldpath.Path, set *fieldpath.Set) {
|
||||
if lhs.MapValue == nil && rhs.MapValue == nil {
|
||||
// Both non-maps, nothing added, do nothing.
|
||||
} else if lhs.MapValue == nil && rhs.MapValue != nil {
|
||||
// From leaf to map, add leaf fields of map.
|
||||
fieldsetDeduced(rhs, path, set)
|
||||
} else if lhs.MapValue != nil && rhs.MapValue == nil {
|
||||
// Went from map to field, add field.
|
||||
set.Insert(path)
|
||||
} else {
|
||||
// Both are maps.
|
||||
for i := range rhs.MapValue.Items {
|
||||
child := rhs.MapValue.Items[i]
|
||||
np := path.Copy()
|
||||
np = append(np, fieldpath.PathElement{FieldName: &child.Name})
|
||||
|
||||
if v, ok := lhs.MapValue.Get(child.Name); ok {
|
||||
added(v.Value, child.Value, np, set)
|
||||
} else {
|
||||
fieldsetDeduced(child.Value, np, set)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func modified(lhs, rhs value.Value, path fieldpath.Path, set *fieldpath.Set) {
|
||||
if lhs.MapValue == nil && rhs.MapValue == nil {
|
||||
if !reflect.DeepEqual(lhs, rhs) {
|
||||
set.Insert(path)
|
||||
}
|
||||
} else if lhs.MapValue != nil && rhs.MapValue != nil {
|
||||
// Both are maps.
|
||||
for i := range rhs.MapValue.Items {
|
||||
child := rhs.MapValue.Items[i]
|
||||
|
||||
v, ok := lhs.MapValue.Get(child.Name)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
np := path.Copy()
|
||||
np = append(np, fieldpath.PathElement{FieldName: &child.Name})
|
||||
modified(v.Value, child.Value, np, set)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveItems does nothing because all lists in a deducedTypedValue are considered atomic,
|
||||
// and there are no maps because it is indistinguishable from a struct.
|
||||
func (dv deducedTypedValue) RemoveItems(_ *fieldpath.Set) TypedValue {
|
||||
return dv
|
||||
}
|
||||
18
vendor/sigs.k8s.io/structured-merge-diff/typed/doc.go
generated
vendored
Normal file
18
vendor/sigs.k8s.io/structured-merge-diff/typed/doc.go
generated
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 typed contains logic for operating on values with given schemas.
|
||||
package typed
|
||||
249
vendor/sigs.k8s.io/structured-merge-diff/typed/helpers.go
generated
vendored
Normal file
249
vendor/sigs.k8s.io/structured-merge-diff/typed/helpers.go
generated
vendored
Normal file
|
|
@ -0,0 +1,249 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 typed
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
// ValidationError reports an error about a particular field
|
||||
type ValidationError struct {
|
||||
Path fieldpath.Path
|
||||
ErrorMessage string
|
||||
}
|
||||
|
||||
// Error returns a human readable error message.
|
||||
func (ve ValidationError) Error() string {
|
||||
if len(ve.Path) == 0 {
|
||||
return ve.ErrorMessage
|
||||
}
|
||||
return fmt.Sprintf("%s: %v", ve.Path, ve.ErrorMessage)
|
||||
}
|
||||
|
||||
// ValidationErrors accumulates multiple validation error messages.
|
||||
type ValidationErrors []ValidationError
|
||||
|
||||
// Error returns a human readable error message reporting each error in the
|
||||
// list.
|
||||
func (errs ValidationErrors) Error() string {
|
||||
if len(errs) == 1 {
|
||||
return errs[0].Error()
|
||||
}
|
||||
messages := []string{"errors:"}
|
||||
for _, e := range errs {
|
||||
messages = append(messages, " "+e.Error())
|
||||
}
|
||||
return strings.Join(messages, "\n")
|
||||
}
|
||||
|
||||
// errorFormatter makes it easy to keep a list of validation errors. They
|
||||
// should all be packed into a single error object before leaving the package
|
||||
// boundary, since it's weird to have functions not return a plain error type.
|
||||
type errorFormatter struct {
|
||||
path fieldpath.Path
|
||||
}
|
||||
|
||||
func (ef *errorFormatter) descend(pe fieldpath.PathElement) {
|
||||
ef.path = append(ef.path, pe)
|
||||
}
|
||||
|
||||
func (ef errorFormatter) errorf(format string, args ...interface{}) ValidationErrors {
|
||||
return ValidationErrors{{
|
||||
Path: append(fieldpath.Path{}, ef.path...),
|
||||
ErrorMessage: fmt.Sprintf(format, args...),
|
||||
}}
|
||||
}
|
||||
|
||||
func (ef errorFormatter) error(err error) ValidationErrors {
|
||||
return ValidationErrors{{
|
||||
Path: append(fieldpath.Path{}, ef.path...),
|
||||
ErrorMessage: err.Error(),
|
||||
}}
|
||||
}
|
||||
|
||||
func (ef errorFormatter) prefixError(prefix string, err error) ValidationErrors {
|
||||
return ValidationErrors{{
|
||||
Path: append(fieldpath.Path{}, ef.path...),
|
||||
ErrorMessage: prefix + err.Error(),
|
||||
}}
|
||||
}
|
||||
|
||||
type atomHandler interface {
|
||||
doScalar(schema.Scalar) ValidationErrors
|
||||
doStruct(schema.Struct) ValidationErrors
|
||||
doList(schema.List) ValidationErrors
|
||||
doMap(schema.Map) ValidationErrors
|
||||
doUntyped(schema.Untyped) ValidationErrors
|
||||
|
||||
errorf(msg string, args ...interface{}) ValidationErrors
|
||||
}
|
||||
|
||||
func resolveSchema(s *schema.Schema, tr schema.TypeRef, ah atomHandler) ValidationErrors {
|
||||
a, ok := s.Resolve(tr)
|
||||
if !ok {
|
||||
return ah.errorf("schema error: no type found matching: %v", *tr.NamedType)
|
||||
}
|
||||
|
||||
switch {
|
||||
case a.Scalar != nil:
|
||||
return ah.doScalar(*a.Scalar)
|
||||
case a.Struct != nil:
|
||||
return ah.doStruct(*a.Struct)
|
||||
case a.List != nil:
|
||||
return ah.doList(*a.List)
|
||||
case a.Map != nil:
|
||||
return ah.doMap(*a.Map)
|
||||
case a.Untyped != nil:
|
||||
return ah.doUntyped(*a.Untyped)
|
||||
}
|
||||
|
||||
name := "inlined"
|
||||
if tr.NamedType != nil {
|
||||
name = "named type: " + *tr.NamedType
|
||||
}
|
||||
|
||||
return ah.errorf("schema error: invalid atom: %v", name)
|
||||
}
|
||||
|
||||
func (ef errorFormatter) validateScalar(t schema.Scalar, v *value.Value, prefix string) (errs ValidationErrors) {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
if v.Null {
|
||||
return nil
|
||||
}
|
||||
switch t {
|
||||
case schema.Numeric:
|
||||
if v.FloatValue == nil && v.IntValue == nil {
|
||||
// TODO: should the schema separate int and float?
|
||||
return ef.errorf("%vexpected numeric (int or float), got %v", prefix, v)
|
||||
}
|
||||
case schema.String:
|
||||
if v.StringValue == nil {
|
||||
return ef.errorf("%vexpected string, got %v", prefix, v)
|
||||
}
|
||||
case schema.Boolean:
|
||||
if v.BooleanValue == nil {
|
||||
return ef.errorf("%vexpected boolean, got %v", prefix, v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns the list, or an error. Reminder: nil is a valid list and might be returned.
|
||||
func listValue(val value.Value) (*value.List, error) {
|
||||
switch {
|
||||
case val.Null:
|
||||
// Null is a valid list.
|
||||
return nil, nil
|
||||
case val.ListValue != nil:
|
||||
return val.ListValue, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("expected list, got %v", val)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the map, or an error. Reminder: nil is a valid map and might be returned.
|
||||
func mapOrStructValue(val value.Value, typeName string) (*value.Map, error) {
|
||||
switch {
|
||||
case val.Null:
|
||||
return nil, nil
|
||||
case val.MapValue != nil:
|
||||
return val.MapValue, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("expected %v, got %v", typeName, val)
|
||||
}
|
||||
}
|
||||
|
||||
func (ef errorFormatter) rejectExtraStructFields(m *value.Map, allowedNames map[string]struct{}, prefix string) (errs ValidationErrors) {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
for _, f := range m.Items {
|
||||
if _, allowed := allowedNames[f.Name]; !allowed {
|
||||
errs = append(errs, ef.errorf("%vfield %v is not mentioned in the schema", prefix, f.Name)...)
|
||||
}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func keyedAssociativeListItemToPathElement(list schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
|
||||
pe := fieldpath.PathElement{}
|
||||
if child.Null {
|
||||
// For now, the keys are required which means that null entries
|
||||
// are illegal.
|
||||
return pe, errors.New("associative list with keys may not have a null element")
|
||||
}
|
||||
if child.MapValue == nil {
|
||||
return pe, errors.New("associative list with keys may not have non-map elements")
|
||||
}
|
||||
for _, fieldName := range list.Keys {
|
||||
var fieldValue value.Value
|
||||
field, ok := child.MapValue.Get(fieldName)
|
||||
if ok {
|
||||
fieldValue = field.Value
|
||||
} else {
|
||||
// Treat keys as required.
|
||||
return pe, fmt.Errorf("associative list with keys has an element that omits key field %q", fieldName)
|
||||
}
|
||||
pe.Key = append(pe.Key, value.Field{
|
||||
Name: fieldName,
|
||||
Value: fieldValue,
|
||||
})
|
||||
}
|
||||
return pe, nil
|
||||
}
|
||||
|
||||
func setItemToPathElement(list schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
|
||||
pe := fieldpath.PathElement{}
|
||||
switch {
|
||||
case child.MapValue != nil:
|
||||
// TODO: atomic maps should be acceptable.
|
||||
return pe, errors.New("associative list without keys has an element that's a map type")
|
||||
case child.ListValue != nil:
|
||||
// Should we support a set of lists? For the moment
|
||||
// let's say we don't.
|
||||
// TODO: atomic lists should be acceptable.
|
||||
return pe, errors.New("not supported: associative list with lists as elements")
|
||||
case child.Null:
|
||||
return pe, errors.New("associative list without keys has an element that's an explicit null")
|
||||
default:
|
||||
// We are a set type.
|
||||
pe.Value = &child
|
||||
return pe, nil
|
||||
}
|
||||
}
|
||||
|
||||
func listItemToPathElement(list schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
|
||||
if list.ElementRelationship == schema.Associative {
|
||||
if len(list.Keys) > 0 {
|
||||
return keyedAssociativeListItemToPathElement(list, index, child)
|
||||
}
|
||||
|
||||
// If there's no keys, then we must be a set of primitives.
|
||||
return setItemToPathElement(list, index, child)
|
||||
}
|
||||
|
||||
// Use the index as a key for atomic lists.
|
||||
return fieldpath.PathElement{Index: &index}, nil
|
||||
}
|
||||
410
vendor/sigs.k8s.io/structured-merge-diff/typed/merge.go
generated
vendored
Normal file
410
vendor/sigs.k8s.io/structured-merge-diff/typed/merge.go
generated
vendored
Normal file
|
|
@ -0,0 +1,410 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 typed
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
type mergingWalker struct {
|
||||
errorFormatter
|
||||
lhs *value.Value
|
||||
rhs *value.Value
|
||||
schema *schema.Schema
|
||||
typeRef schema.TypeRef
|
||||
|
||||
// How to merge. Called after schema validation for all leaf fields.
|
||||
rule mergeRule
|
||||
|
||||
// If set, called after non-leaf items have been merged. (`out` is
|
||||
// probably already set.)
|
||||
postItemHook mergeRule
|
||||
|
||||
// output of the merge operation (nil if none)
|
||||
out *value.Value
|
||||
|
||||
// internal housekeeping--don't set when constructing.
|
||||
inLeaf bool // Set to true if we're in a "big leaf"--atomic map/list
|
||||
}
|
||||
|
||||
// merge rules examine w.lhs and w.rhs (up to one of which may be nil) and
|
||||
// optionally set w.out. If lhs and rhs are both set, they will be of
|
||||
// comparable type.
|
||||
type mergeRule func(w *mergingWalker)
|
||||
|
||||
var (
|
||||
ruleKeepRHS = mergeRule(func(w *mergingWalker) {
|
||||
if w.rhs != nil {
|
||||
v := *w.rhs
|
||||
w.out = &v
|
||||
} else if w.lhs != nil {
|
||||
v := *w.lhs
|
||||
w.out = &v
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// merge sets w.out.
|
||||
func (w *mergingWalker) merge() ValidationErrors {
|
||||
if w.lhs == nil && w.rhs == nil {
|
||||
// check this condidition here instead of everywhere below.
|
||||
return w.errorf("at least one of lhs and rhs must be provided")
|
||||
}
|
||||
errs := resolveSchema(w.schema, w.typeRef, w)
|
||||
if !w.inLeaf && w.postItemHook != nil {
|
||||
w.postItemHook(w)
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
// doLeaf should be called on leaves before descending into children, if there
|
||||
// will be a descent. It modifies w.inLeaf.
|
||||
func (w *mergingWalker) doLeaf() {
|
||||
if w.inLeaf {
|
||||
// We're in a "big leaf", an atomic map or list. Ignore
|
||||
// subsequent leaves.
|
||||
return
|
||||
}
|
||||
w.inLeaf = true
|
||||
|
||||
// We don't recurse into leaf fields for merging.
|
||||
w.rule(w)
|
||||
}
|
||||
|
||||
func (w *mergingWalker) doScalar(t schema.Scalar) (errs ValidationErrors) {
|
||||
errs = append(errs, w.validateScalar(t, w.lhs, "lhs: ")...)
|
||||
errs = append(errs, w.validateScalar(t, w.rhs, "rhs: ")...)
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
// All scalars are leaf fields.
|
||||
w.doLeaf()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *mergingWalker) prepareDescent(pe fieldpath.PathElement, tr schema.TypeRef) *mergingWalker {
|
||||
w2 := *w
|
||||
w2.typeRef = tr
|
||||
w2.errorFormatter.descend(pe)
|
||||
w2.lhs = nil
|
||||
w2.rhs = nil
|
||||
w2.out = nil
|
||||
return &w2
|
||||
}
|
||||
|
||||
func (w *mergingWalker) visitStructFields(t schema.Struct, lhs, rhs *value.Map) (errs ValidationErrors) {
|
||||
out := &value.Map{}
|
||||
|
||||
valOrNil := func(m *value.Map, name string) *value.Value {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
val, ok := m.Get(name)
|
||||
if ok {
|
||||
return &val.Value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
allowedNames := map[string]struct{}{}
|
||||
for i := range t.Fields {
|
||||
// I don't want to use the loop variable since a reference
|
||||
// might outlive the loop iteration (in an error message).
|
||||
f := t.Fields[i]
|
||||
allowedNames[f.Name] = struct{}{}
|
||||
w2 := w.prepareDescent(fieldpath.PathElement{FieldName: &f.Name}, f.Type)
|
||||
w2.lhs = valOrNil(lhs, f.Name)
|
||||
w2.rhs = valOrNil(rhs, f.Name)
|
||||
if w2.lhs == nil && w2.rhs == nil {
|
||||
// All fields are optional
|
||||
continue
|
||||
}
|
||||
if newErrs := w2.merge(); len(newErrs) > 0 {
|
||||
errs = append(errs, newErrs...)
|
||||
} else if w2.out != nil {
|
||||
out.Set(f.Name, *w2.out)
|
||||
}
|
||||
}
|
||||
|
||||
// All fields may be optional, but unknown fields are not allowed.
|
||||
errs = append(errs, w.rejectExtraStructFields(lhs, allowedNames, "lhs: ")...)
|
||||
errs = append(errs, w.rejectExtraStructFields(rhs, allowedNames, "rhs: ")...)
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
if len(out.Items) > 0 {
|
||||
w.out = &value.Value{MapValue: out}
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (w *mergingWalker) derefMapOrStruct(prefix, typeName string, v *value.Value, dest **value.Map) (errs ValidationErrors) {
|
||||
// taking dest as input so that it can be called as a one-liner with
|
||||
// append.
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
m, err := mapOrStructValue(*v, typeName)
|
||||
if err != nil {
|
||||
return w.prefixError(prefix, err)
|
||||
}
|
||||
*dest = m
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *mergingWalker) doStruct(t schema.Struct) (errs ValidationErrors) {
|
||||
var lhs, rhs *value.Map
|
||||
errs = append(errs, w.derefMapOrStruct("lhs: ", "struct", w.lhs, &lhs)...)
|
||||
errs = append(errs, w.derefMapOrStruct("rhs: ", "struct", w.rhs, &rhs)...)
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
// If both lhs and rhs are empty/null, treat it as a
|
||||
// leaf: this helps preserve the empty/null
|
||||
// distinction.
|
||||
emptyPromoteToLeaf := (lhs == nil || len(lhs.Items) == 0) &&
|
||||
(rhs == nil || len(rhs.Items) == 0)
|
||||
|
||||
if t.ElementRelationship == schema.Atomic || emptyPromoteToLeaf {
|
||||
w.doLeaf()
|
||||
return nil
|
||||
}
|
||||
|
||||
if lhs == nil && rhs == nil {
|
||||
// nil is a valid map!
|
||||
return nil
|
||||
}
|
||||
|
||||
errs = w.visitStructFields(t, lhs, rhs)
|
||||
|
||||
// TODO: Check unions.
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (w *mergingWalker) visitListItems(t schema.List, lhs, rhs *value.List) (errs ValidationErrors) {
|
||||
out := &value.List{}
|
||||
|
||||
// TODO: ordering is totally wrong.
|
||||
// TODO: might as well make the map order work the same way.
|
||||
|
||||
// This is a cheap hack to at least make the output order stable.
|
||||
rhsOrder := []fieldpath.PathElement{}
|
||||
|
||||
// First, collect all RHS children.
|
||||
observedRHS := map[string]value.Value{}
|
||||
if rhs != nil {
|
||||
for i, child := range rhs.Items {
|
||||
pe, err := listItemToPathElement(t, i, child)
|
||||
if err != nil {
|
||||
errs = append(errs, w.errorf("rhs: element %v: %v", i, err.Error())...)
|
||||
// If we can't construct the path element, we can't
|
||||
// even report errors deeper in the schema, so bail on
|
||||
// this element.
|
||||
continue
|
||||
}
|
||||
keyStr := pe.String()
|
||||
if _, found := observedRHS[keyStr]; found {
|
||||
errs = append(errs, w.errorf("rhs: duplicate entries for key %v", keyStr)...)
|
||||
}
|
||||
observedRHS[keyStr] = child
|
||||
rhsOrder = append(rhsOrder, pe)
|
||||
}
|
||||
}
|
||||
|
||||
// Then merge with LHS children.
|
||||
observedLHS := map[string]struct{}{}
|
||||
if lhs != nil {
|
||||
for i, child := range lhs.Items {
|
||||
pe, err := listItemToPathElement(t, i, child)
|
||||
if err != nil {
|
||||
errs = append(errs, w.errorf("lhs: element %v: %v", i, err.Error())...)
|
||||
// If we can't construct the path element, we can't
|
||||
// even report errors deeper in the schema, so bail on
|
||||
// this element.
|
||||
continue
|
||||
}
|
||||
keyStr := pe.String()
|
||||
if _, found := observedLHS[keyStr]; found {
|
||||
errs = append(errs, w.errorf("lhs: duplicate entries for key %v", keyStr)...)
|
||||
continue
|
||||
}
|
||||
observedLHS[keyStr] = struct{}{}
|
||||
w2 := w.prepareDescent(pe, t.ElementType)
|
||||
w2.lhs = &child
|
||||
if rchild, ok := observedRHS[keyStr]; ok {
|
||||
w2.rhs = &rchild
|
||||
}
|
||||
if newErrs := w2.merge(); len(newErrs) > 0 {
|
||||
errs = append(errs, newErrs...)
|
||||
} else if w2.out != nil {
|
||||
out.Items = append(out.Items, *w2.out)
|
||||
}
|
||||
// Keep track of children that have been handled
|
||||
delete(observedRHS, keyStr)
|
||||
}
|
||||
}
|
||||
|
||||
for _, rhsToCheck := range rhsOrder {
|
||||
if unmergedChild, ok := observedRHS[rhsToCheck.String()]; ok {
|
||||
w2 := w.prepareDescent(rhsToCheck, t.ElementType)
|
||||
w2.rhs = &unmergedChild
|
||||
if newErrs := w2.merge(); len(newErrs) > 0 {
|
||||
errs = append(errs, newErrs...)
|
||||
} else if w2.out != nil {
|
||||
out.Items = append(out.Items, *w2.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(out.Items) > 0 {
|
||||
w.out = &value.Value{ListValue: out}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (w *mergingWalker) derefList(prefix string, v *value.Value, dest **value.List) (errs ValidationErrors) {
|
||||
// taking dest as input so that it can be called as a one-liner with
|
||||
// append.
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
l, err := listValue(*v)
|
||||
if err != nil {
|
||||
return w.prefixError(prefix, err)
|
||||
}
|
||||
*dest = l
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *mergingWalker) doList(t schema.List) (errs ValidationErrors) {
|
||||
var lhs, rhs *value.List
|
||||
errs = append(errs, w.derefList("lhs: ", w.lhs, &lhs)...)
|
||||
errs = append(errs, w.derefList("rhs: ", w.rhs, &rhs)...)
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
// If both lhs and rhs are empty/null, treat it as a
|
||||
// leaf: this helps preserve the empty/null
|
||||
// distinction.
|
||||
emptyPromoteToLeaf := (lhs == nil || len(lhs.Items) == 0) &&
|
||||
(rhs == nil || len(rhs.Items) == 0)
|
||||
|
||||
if t.ElementRelationship == schema.Atomic || emptyPromoteToLeaf {
|
||||
w.doLeaf()
|
||||
return nil
|
||||
}
|
||||
|
||||
if lhs == nil && rhs == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
errs = w.visitListItems(t, lhs, rhs)
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (w *mergingWalker) visitMapItems(t schema.Map, lhs, rhs *value.Map) (errs ValidationErrors) {
|
||||
out := &value.Map{}
|
||||
|
||||
if lhs != nil {
|
||||
for _, litem := range lhs.Items {
|
||||
name := litem.Name
|
||||
w2 := w.prepareDescent(fieldpath.PathElement{FieldName: &name}, t.ElementType)
|
||||
w2.lhs = &litem.Value
|
||||
if rhs != nil {
|
||||
if ritem, ok := rhs.Get(litem.Name); ok {
|
||||
w2.rhs = &ritem.Value
|
||||
}
|
||||
}
|
||||
if newErrs := w2.merge(); len(newErrs) > 0 {
|
||||
errs = append(errs, newErrs...)
|
||||
} else if w2.out != nil {
|
||||
out.Set(name, *w2.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if rhs != nil {
|
||||
for _, ritem := range rhs.Items {
|
||||
if lhs != nil {
|
||||
if _, ok := lhs.Get(ritem.Name); ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
name := ritem.Name
|
||||
w2 := w.prepareDescent(fieldpath.PathElement{FieldName: &name}, t.ElementType)
|
||||
w2.rhs = &ritem.Value
|
||||
if newErrs := w2.merge(); len(newErrs) > 0 {
|
||||
errs = append(errs, newErrs...)
|
||||
} else if w2.out != nil {
|
||||
out.Set(name, *w2.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(out.Items) > 0 {
|
||||
w.out = &value.Value{MapValue: out}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (w *mergingWalker) doMap(t schema.Map) (errs ValidationErrors) {
|
||||
var lhs, rhs *value.Map
|
||||
errs = append(errs, w.derefMapOrStruct("lhs: ", "map", w.lhs, &lhs)...)
|
||||
errs = append(errs, w.derefMapOrStruct("rhs: ", "map", w.rhs, &rhs)...)
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
// If both lhs and rhs are empty/null, treat it as a
|
||||
// leaf: this helps preserve the empty/null
|
||||
// distinction.
|
||||
emptyPromoteToLeaf := (lhs == nil || len(lhs.Items) == 0) &&
|
||||
(rhs == nil || len(rhs.Items) == 0)
|
||||
|
||||
if t.ElementRelationship == schema.Atomic || emptyPromoteToLeaf {
|
||||
w.doLeaf()
|
||||
return nil
|
||||
}
|
||||
|
||||
if lhs == nil && rhs == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
errs = w.visitMapItems(t, lhs, rhs)
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (w *mergingWalker) doUntyped(t schema.Untyped) (errs ValidationErrors) {
|
||||
if t.ElementRelationship == "" || t.ElementRelationship == schema.Atomic {
|
||||
// Untyped sections allow anything, and are considered leaf
|
||||
// fields.
|
||||
w.doLeaf()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
147
vendor/sigs.k8s.io/structured-merge-diff/typed/parser.go
generated
vendored
Normal file
147
vendor/sigs.k8s.io/structured-merge-diff/typed/parser.go
generated
vendored
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 typed
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
// YAMLObject is an object encoded in YAML.
|
||||
type YAMLObject string
|
||||
|
||||
// Parser implements YAMLParser and allows introspecting the schema.
|
||||
type Parser struct {
|
||||
Schema schema.Schema
|
||||
}
|
||||
|
||||
// create builds an unvalidated parser.
|
||||
func create(schema YAMLObject) (*Parser, error) {
|
||||
p := Parser{}
|
||||
err := yaml.Unmarshal([]byte(schema), &p.Schema)
|
||||
return &p, err
|
||||
}
|
||||
|
||||
func createOrDie(schema YAMLObject) *Parser {
|
||||
p, err := create(schema)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to create parser: %v", err))
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
var ssParser = createOrDie(YAMLObject(schema.SchemaSchemaYAML))
|
||||
|
||||
// NewParser will build a YAMLParser from a schema. The schema is validated.
|
||||
func NewParser(schema YAMLObject) (*Parser, error) {
|
||||
_, err := ssParser.Type("schema").FromYAML(schema)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to validate schema: %v", err)
|
||||
}
|
||||
return create(schema)
|
||||
}
|
||||
|
||||
// TypeNames returns a list of types this parser understands.
|
||||
func (p *Parser) TypeNames() (names []string) {
|
||||
for _, td := range p.Schema.Types {
|
||||
names = append(names, td.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// Type returns a helper which can produce objects of the given type. Any
|
||||
// errors are deferred until a further function is called.
|
||||
func (p *Parser) Type(name string) ParseableType {
|
||||
return &parseableType{
|
||||
parser: p,
|
||||
typename: name,
|
||||
}
|
||||
}
|
||||
|
||||
// ParseableType allows for easy production of typed objects.
|
||||
type ParseableType interface {
|
||||
IsValid() bool
|
||||
FromYAML(YAMLObject) (TypedValue, error)
|
||||
FromUnstructured(interface{}) (TypedValue, error)
|
||||
}
|
||||
|
||||
type parseableType struct {
|
||||
parser *Parser
|
||||
typename string
|
||||
}
|
||||
|
||||
var _ ParseableType = &parseableType{}
|
||||
|
||||
// IsValid return true if p's schema and typename are valid.
|
||||
func (p *parseableType) IsValid() bool {
|
||||
_, ok := p.parser.Schema.Resolve(schema.TypeRef{NamedType: &p.typename})
|
||||
return ok
|
||||
}
|
||||
|
||||
// FromYAML parses a yaml string into an object with the current schema
|
||||
// and the type "typename" or an error if validation fails.
|
||||
func (p *parseableType) FromYAML(object YAMLObject) (TypedValue, error) {
|
||||
v, err := value.FromYAML([]byte(object))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return AsTyped(v, &p.parser.Schema, p.typename)
|
||||
}
|
||||
|
||||
// FromUnstructured converts a go interface to a TypedValue. It will return an
|
||||
// error if the resulting object fails schema validation.
|
||||
func (p *parseableType) FromUnstructured(in interface{}) (TypedValue, error) {
|
||||
v, err := value.FromUnstructured(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return AsTyped(v, &p.parser.Schema, p.typename)
|
||||
}
|
||||
|
||||
// DeducedParseableType is a ParseableType that deduces the type from
|
||||
// the content of the object.
|
||||
type DeducedParseableType struct{}
|
||||
|
||||
var _ ParseableType = DeducedParseableType{}
|
||||
|
||||
// IsValid always returns true for a DeducedParseableType.
|
||||
func (p DeducedParseableType) IsValid() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// FromYAML parses a yaml string into an object and deduces the type for
|
||||
// that object.
|
||||
func (p DeducedParseableType) FromYAML(object YAMLObject) (TypedValue, error) {
|
||||
v, err := value.FromYAML([]byte(object))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return AsTypedDeduced(v), nil
|
||||
}
|
||||
|
||||
// FromUnstructured converts a go interface to a TypedValue. It will return an
|
||||
// error if the input object uses un-handled types.
|
||||
func (p DeducedParseableType) FromUnstructured(in interface{}) (TypedValue, error) {
|
||||
v, err := value.FromUnstructured(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return AsTypedDeduced(v), nil
|
||||
}
|
||||
127
vendor/sigs.k8s.io/structured-merge-diff/typed/remove.go
generated
vendored
Normal file
127
vendor/sigs.k8s.io/structured-merge-diff/typed/remove.go
generated
vendored
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
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 typed
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
type removingWalker struct {
|
||||
value *value.Value
|
||||
schema *schema.Schema
|
||||
toRemove *fieldpath.Set
|
||||
}
|
||||
|
||||
func removeItemsWithSchema(value *value.Value, toRemove *fieldpath.Set, schema *schema.Schema, typeRef schema.TypeRef) {
|
||||
w := &removingWalker{
|
||||
value: value,
|
||||
schema: schema,
|
||||
toRemove: toRemove,
|
||||
}
|
||||
resolveSchema(schema, typeRef, w)
|
||||
}
|
||||
|
||||
// doLeaf should be called on leaves before descending into children, if there
|
||||
// will be a descent. It modifies w.inLeaf.
|
||||
func (w *removingWalker) doLeaf() ValidationErrors { return nil }
|
||||
|
||||
func (w *removingWalker) doScalar(t schema.Scalar) ValidationErrors { return nil }
|
||||
|
||||
func (w *removingWalker) doStruct(t schema.Struct) ValidationErrors {
|
||||
s := w.value.MapValue
|
||||
|
||||
// If struct is null, empty, or atomic just return
|
||||
if s == nil || len(s.Items) == 0 || t.ElementRelationship == schema.Atomic {
|
||||
return nil
|
||||
}
|
||||
|
||||
fieldTypes := map[string]schema.TypeRef{}
|
||||
for _, structField := range t.Fields {
|
||||
fieldTypes[structField.Name] = structField.Type
|
||||
}
|
||||
|
||||
for i, _ := range s.Items {
|
||||
item := s.Items[i]
|
||||
pe := fieldpath.PathElement{FieldName: &item.Name}
|
||||
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
|
||||
removeItemsWithSchema(&s.Items[i].Value, subset, w.schema, fieldTypes[item.Name])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *removingWalker) doList(t schema.List) (errs ValidationErrors) {
|
||||
l := w.value.ListValue
|
||||
|
||||
// If list is null, empty, or atomic just return
|
||||
if l == nil || len(l.Items) == 0 || t.ElementRelationship == schema.Atomic {
|
||||
return nil
|
||||
}
|
||||
|
||||
newItems := []value.Value{}
|
||||
for i, _ := range l.Items {
|
||||
item := l.Items[i]
|
||||
// Ignore error because we have already validated this list
|
||||
pe, _ := listItemToPathElement(t, i, item)
|
||||
path, _ := fieldpath.MakePath(pe)
|
||||
if w.toRemove.Has(path) {
|
||||
continue
|
||||
}
|
||||
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
|
||||
removeItemsWithSchema(&l.Items[i], subset, w.schema, t.ElementType)
|
||||
}
|
||||
newItems = append(newItems, l.Items[i])
|
||||
}
|
||||
l.Items = newItems
|
||||
if len(l.Items) == 0 {
|
||||
w.value.ListValue = nil
|
||||
w.value.Null = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *removingWalker) doMap(t schema.Map) ValidationErrors {
|
||||
m := w.value.MapValue
|
||||
|
||||
// If map is null, empty, or atomic just return
|
||||
if m == nil || len(m.Items) == 0 || t.ElementRelationship == schema.Atomic {
|
||||
return nil
|
||||
}
|
||||
|
||||
newMap := &value.Map{}
|
||||
for i, _ := range m.Items {
|
||||
item := m.Items[i]
|
||||
pe := fieldpath.PathElement{FieldName: &item.Name}
|
||||
path, _ := fieldpath.MakePath(pe)
|
||||
if w.toRemove.Has(path) {
|
||||
continue
|
||||
}
|
||||
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
|
||||
removeItemsWithSchema(&m.Items[i].Value, subset, w.schema, t.ElementType)
|
||||
}
|
||||
newMap.Set(item.Name, m.Items[i].Value)
|
||||
}
|
||||
w.value.MapValue = newMap
|
||||
if len(w.value.MapValue.Items) == 0 {
|
||||
w.value.MapValue = nil
|
||||
w.value.Null = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*removingWalker) doUntyped(_ schema.Untyped) ValidationErrors { return nil }
|
||||
|
||||
func (*removingWalker) errorf(_ string, _ ...interface{}) ValidationErrors { return nil }
|
||||
245
vendor/sigs.k8s.io/structured-merge-diff/typed/typed.go
generated
vendored
Normal file
245
vendor/sigs.k8s.io/structured-merge-diff/typed/typed.go
generated
vendored
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 typed
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
// TypedValue is a value with an associated type.
|
||||
type TypedValue interface {
|
||||
// AsValue removes the type from the TypedValue and only keeps the value.
|
||||
AsValue() *value.Value
|
||||
// Validate returns an error with a list of every spec violation.
|
||||
Validate() error
|
||||
// ToFieldSet creates a set containing every leaf field and item mentioned, or
|
||||
// validation errors, if any were encountered.
|
||||
ToFieldSet() (*fieldpath.Set, error)
|
||||
// Merge returns the result of merging tv and pso ("partially specified
|
||||
// object") together. Of note:
|
||||
// * No fields can be removed by this operation.
|
||||
// * If both tv and pso specify a given leaf field, the result will keep pso's
|
||||
// value.
|
||||
// * Container typed elements will have their items ordered:
|
||||
// * like tv, if pso doesn't change anything in the container
|
||||
// * like pso, if pso does change something in the container.
|
||||
// tv and pso must both be of the same type (their Schema and TypeRef must
|
||||
// match), or an error will be returned. Validation errors will be returned if
|
||||
// the objects don't conform to the schema.
|
||||
Merge(pso TypedValue) (TypedValue, error)
|
||||
// Compare compares the two objects. See the comments on the `Comparison`
|
||||
// struct for details on the return value.
|
||||
//
|
||||
// tv and rhs must both be of the same type (their Schema and TypeRef must
|
||||
// match), or an error will be returned. Validation errors will be returned if
|
||||
// the objects don't conform to the schema.
|
||||
Compare(rhs TypedValue) (c *Comparison, err error)
|
||||
// RemoveItems removes each provided list or map item from the value.
|
||||
RemoveItems(items *fieldpath.Set) TypedValue
|
||||
}
|
||||
|
||||
// AsTyped accepts a value and a type and returns a TypedValue. 'v' must have
|
||||
// type 'typeName' in the schema. An error is returned if the v doesn't conform
|
||||
// to the schema.
|
||||
func AsTyped(v value.Value, s *schema.Schema, typeName string) (TypedValue, error) {
|
||||
tv := typedValue{
|
||||
value: v,
|
||||
typeRef: schema.TypeRef{NamedType: &typeName},
|
||||
schema: s,
|
||||
}
|
||||
if err := tv.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tv, nil
|
||||
}
|
||||
|
||||
// AsTypeUnvalidated is just like AsTyped, but doesn't validate that the type
|
||||
// conforms to the schema, for cases where that has already been checked or
|
||||
// where you're going to call a method that validates as a side-effect (like
|
||||
// ToFieldSet).
|
||||
func AsTypedUnvalidated(v value.Value, s *schema.Schema, typeName string) TypedValue {
|
||||
tv := typedValue{
|
||||
value: v,
|
||||
typeRef: schema.TypeRef{NamedType: &typeName},
|
||||
schema: s,
|
||||
}
|
||||
return tv
|
||||
}
|
||||
|
||||
// typedValue is a value of some specific type.
|
||||
type typedValue struct {
|
||||
value value.Value
|
||||
typeRef schema.TypeRef
|
||||
schema *schema.Schema
|
||||
}
|
||||
|
||||
var _ TypedValue = typedValue{}
|
||||
|
||||
func (tv typedValue) AsValue() *value.Value {
|
||||
return &tv.value
|
||||
}
|
||||
|
||||
func (tv typedValue) Validate() error {
|
||||
if errs := tv.walker().validate(); len(errs) != 0 {
|
||||
return errs
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tv typedValue) ToFieldSet() (*fieldpath.Set, error) {
|
||||
s := fieldpath.NewSet()
|
||||
w := tv.walker()
|
||||
w.leafFieldCallback = func(p fieldpath.Path) { s.Insert(p) }
|
||||
w.nodeFieldCallback = func(p fieldpath.Path) { s.Insert(p) }
|
||||
if errs := w.validate(); len(errs) != 0 {
|
||||
return nil, errs
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (tv typedValue) Merge(pso TypedValue) (TypedValue, error) {
|
||||
tpso, ok := pso.(typedValue)
|
||||
if !ok {
|
||||
return nil, errorFormatter{}.
|
||||
errorf("can't merge typedValue with %T", pso)
|
||||
}
|
||||
return merge(tv, tpso, ruleKeepRHS, nil)
|
||||
}
|
||||
|
||||
func (tv typedValue) Compare(rhs TypedValue) (c *Comparison, err error) {
|
||||
trhs, ok := rhs.(typedValue)
|
||||
if !ok {
|
||||
return nil, errorFormatter{}.
|
||||
errorf("can't compare typedValue with %T", rhs)
|
||||
}
|
||||
c = &Comparison{
|
||||
Removed: fieldpath.NewSet(),
|
||||
Modified: fieldpath.NewSet(),
|
||||
Added: fieldpath.NewSet(),
|
||||
}
|
||||
c.Merged, err = merge(tv, trhs, func(w *mergingWalker) {
|
||||
if w.lhs == nil {
|
||||
c.Added.Insert(w.path)
|
||||
} else if w.rhs == nil {
|
||||
c.Removed.Insert(w.path)
|
||||
} else if !reflect.DeepEqual(w.rhs, w.lhs) {
|
||||
// TODO: reflect.DeepEqual is not sufficient for this.
|
||||
// Need to implement equality check on the value type.
|
||||
c.Modified.Insert(w.path)
|
||||
}
|
||||
|
||||
ruleKeepRHS(w)
|
||||
}, func(w *mergingWalker) {
|
||||
if w.lhs == nil {
|
||||
c.Added.Insert(w.path)
|
||||
} else if w.rhs == nil {
|
||||
c.Removed.Insert(w.path)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// RemoveItems removes each provided list or map item from the value.
|
||||
func (tv typedValue) RemoveItems(items *fieldpath.Set) TypedValue {
|
||||
copied := tv
|
||||
copied.value, _ = value.FromUnstructured(tv.value.ToUnstructured(true))
|
||||
removeItemsWithSchema(&copied.value, items, copied.schema, copied.typeRef)
|
||||
return copied
|
||||
}
|
||||
|
||||
func merge(lhs, rhs typedValue, rule, postRule mergeRule) (TypedValue, error) {
|
||||
if lhs.schema != rhs.schema {
|
||||
return nil, errorFormatter{}.
|
||||
errorf("expected objects with types from the same schema")
|
||||
}
|
||||
if !reflect.DeepEqual(lhs.typeRef, rhs.typeRef) {
|
||||
return nil, errorFormatter{}.
|
||||
errorf("expected objects of the same type, but got %v and %v", lhs.typeRef, rhs.typeRef)
|
||||
}
|
||||
|
||||
mw := mergingWalker{
|
||||
lhs: &lhs.value,
|
||||
rhs: &rhs.value,
|
||||
schema: lhs.schema,
|
||||
typeRef: lhs.typeRef,
|
||||
rule: rule,
|
||||
postItemHook: postRule,
|
||||
}
|
||||
errs := mw.merge()
|
||||
if len(errs) > 0 {
|
||||
return nil, errs
|
||||
}
|
||||
|
||||
out := typedValue{
|
||||
schema: lhs.schema,
|
||||
typeRef: lhs.typeRef,
|
||||
}
|
||||
if mw.out == nil {
|
||||
out.value = value.Value{Null: true}
|
||||
} else {
|
||||
out.value = *mw.out
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Comparison is the return value of a TypedValue.Compare() operation.
|
||||
//
|
||||
// No field will appear in more than one of the three fieldsets. If all of the
|
||||
// fieldsets are empty, then the objects must have been equal.
|
||||
type Comparison struct {
|
||||
// Merged is the result of merging the two objects, as explained in the
|
||||
// comments on TypedValue.Merge().
|
||||
Merged TypedValue
|
||||
|
||||
// Removed contains any fields removed by rhs (the right-hand-side
|
||||
// object in the comparison).
|
||||
Removed *fieldpath.Set
|
||||
// Modified contains fields present in both objects but different.
|
||||
Modified *fieldpath.Set
|
||||
// Added contains any fields added by rhs.
|
||||
Added *fieldpath.Set
|
||||
}
|
||||
|
||||
// IsSame returns true if the comparison returned no changes (the two
|
||||
// compared objects are similar).
|
||||
func (c *Comparison) IsSame() bool {
|
||||
return c.Removed.Empty() && c.Modified.Empty() && c.Added.Empty()
|
||||
}
|
||||
|
||||
// String returns a human readable version of the comparison.
|
||||
func (c *Comparison) String() string {
|
||||
str := fmt.Sprintf("- Merged Object:\n%v\n", c.Merged.AsValue())
|
||||
if !c.Modified.Empty() {
|
||||
str += fmt.Sprintf("- Modified Fields:\n%v\n", c.Modified)
|
||||
}
|
||||
if !c.Added.Empty() {
|
||||
str += fmt.Sprintf("- Added Fields:\n%v\n", c.Added)
|
||||
}
|
||||
if !c.Removed.Empty() {
|
||||
str += fmt.Sprintf("- Removed Fields:\n%v\n", c.Removed)
|
||||
}
|
||||
return str
|
||||
}
|
||||
232
vendor/sigs.k8s.io/structured-merge-diff/typed/validate.go
generated
vendored
Normal file
232
vendor/sigs.k8s.io/structured-merge-diff/typed/validate.go
generated
vendored
Normal file
|
|
@ -0,0 +1,232 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 typed
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
func (tv typedValue) walker() *validatingObjectWalker {
|
||||
return &validatingObjectWalker{
|
||||
value: tv.value,
|
||||
schema: tv.schema,
|
||||
typeRef: tv.typeRef,
|
||||
}
|
||||
}
|
||||
|
||||
type validatingObjectWalker struct {
|
||||
errorFormatter
|
||||
value value.Value
|
||||
schema *schema.Schema
|
||||
typeRef schema.TypeRef
|
||||
|
||||
// If set, this is called on "leaf fields":
|
||||
// * scalars: int/string/float/bool
|
||||
// * atomic maps and lists
|
||||
// * untyped fields
|
||||
leafFieldCallback func(fieldpath.Path)
|
||||
|
||||
// If set, this is called on "node fields":
|
||||
// * list items
|
||||
// * map items
|
||||
nodeFieldCallback func(fieldpath.Path)
|
||||
|
||||
// internal housekeeping--don't set when constructing.
|
||||
inLeaf bool // Set to true if we're in a "big leaf"--atomic map/list
|
||||
}
|
||||
|
||||
func (v validatingObjectWalker) validate() ValidationErrors {
|
||||
return resolveSchema(v.schema, v.typeRef, v)
|
||||
}
|
||||
|
||||
// doLeaf should be called on leaves before descending into children, if there
|
||||
// will be a descent. It modifies v.inLeaf.
|
||||
func (v *validatingObjectWalker) doLeaf() {
|
||||
if v.inLeaf {
|
||||
// We're in a "big leaf", an atomic map or list. Ignore
|
||||
// subsequent leaves.
|
||||
return
|
||||
}
|
||||
v.inLeaf = true
|
||||
|
||||
if v.leafFieldCallback != nil {
|
||||
// At the moment, this is only used to build fieldsets; we can
|
||||
// add more than the path in here if needed.
|
||||
v.leafFieldCallback(v.path)
|
||||
}
|
||||
}
|
||||
|
||||
// doNode should be called on nodes after descending into children
|
||||
func (v *validatingObjectWalker) doNode() {
|
||||
if v.inLeaf {
|
||||
// We're in a "big leaf", an atomic map or list. Ignore
|
||||
// subsequent leaves.
|
||||
return
|
||||
}
|
||||
|
||||
if v.nodeFieldCallback != nil {
|
||||
// At the moment, this is only used to build fieldsets; we can
|
||||
// add more than the path in here if needed.
|
||||
v.nodeFieldCallback(v.path)
|
||||
}
|
||||
}
|
||||
|
||||
func (v validatingObjectWalker) doScalar(t schema.Scalar) ValidationErrors {
|
||||
if errs := v.validateScalar(t, &v.value, ""); len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
// All scalars are leaf fields.
|
||||
v.doLeaf()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v validatingObjectWalker) visitStructFields(t schema.Struct, m *value.Map) (errs ValidationErrors) {
|
||||
allowedNames := map[string]struct{}{}
|
||||
for i := range t.Fields {
|
||||
// I don't want to use the loop variable since a reference
|
||||
// might outlive the loop iteration (in an error message).
|
||||
f := t.Fields[i]
|
||||
allowedNames[f.Name] = struct{}{}
|
||||
child, ok := m.Get(f.Name)
|
||||
if !ok {
|
||||
// All fields are optional
|
||||
continue
|
||||
}
|
||||
v2 := v
|
||||
v2.errorFormatter.descend(fieldpath.PathElement{FieldName: &f.Name})
|
||||
v2.value = child.Value
|
||||
v2.typeRef = f.Type
|
||||
errs = append(errs, v2.validate()...)
|
||||
}
|
||||
|
||||
// All fields may be optional, but unknown fields are not allowed.
|
||||
return append(errs, v.rejectExtraStructFields(m, allowedNames, "")...)
|
||||
}
|
||||
|
||||
func (v validatingObjectWalker) doStruct(t schema.Struct) (errs ValidationErrors) {
|
||||
m, err := mapOrStructValue(v.value, "struct")
|
||||
if err != nil {
|
||||
return v.error(err)
|
||||
}
|
||||
|
||||
if t.ElementRelationship == schema.Atomic {
|
||||
v.doLeaf()
|
||||
}
|
||||
|
||||
if m == nil {
|
||||
// nil is a valid map!
|
||||
return nil
|
||||
}
|
||||
|
||||
errs = v.visitStructFields(t, m)
|
||||
|
||||
// TODO: Check unions.
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v validatingObjectWalker) visitListItems(t schema.List, list *value.List) (errs ValidationErrors) {
|
||||
observedKeys := map[string]struct{}{}
|
||||
for i, child := range list.Items {
|
||||
pe, err := listItemToPathElement(t, i, child)
|
||||
if err != nil {
|
||||
errs = append(errs, v.errorf("element %v: %v", i, err.Error())...)
|
||||
// If we can't construct the path element, we can't
|
||||
// even report errors deeper in the schema, so bail on
|
||||
// this element.
|
||||
continue
|
||||
}
|
||||
keyStr := pe.String()
|
||||
if _, found := observedKeys[keyStr]; found {
|
||||
errs = append(errs, v.errorf("duplicate entries for key %v", keyStr)...)
|
||||
}
|
||||
observedKeys[keyStr] = struct{}{}
|
||||
v2 := v
|
||||
v2.errorFormatter.descend(pe)
|
||||
v2.value = child
|
||||
v2.typeRef = t.ElementType
|
||||
errs = append(errs, v2.validate()...)
|
||||
|
||||
v2.doNode()
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v validatingObjectWalker) doList(t schema.List) (errs ValidationErrors) {
|
||||
list, err := listValue(v.value)
|
||||
if err != nil {
|
||||
return v.error(err)
|
||||
}
|
||||
|
||||
if t.ElementRelationship == schema.Atomic {
|
||||
v.doLeaf()
|
||||
}
|
||||
|
||||
if list == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
errs = v.visitListItems(t, list)
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v validatingObjectWalker) visitMapItems(t schema.Map, m *value.Map) (errs ValidationErrors) {
|
||||
for _, item := range m.Items {
|
||||
v2 := v
|
||||
name := item.Name
|
||||
v2.errorFormatter.descend(fieldpath.PathElement{FieldName: &name})
|
||||
v2.value = item.Value
|
||||
v2.typeRef = t.ElementType
|
||||
errs = append(errs, v2.validate()...)
|
||||
|
||||
v2.doNode()
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v validatingObjectWalker) doMap(t schema.Map) (errs ValidationErrors) {
|
||||
m, err := mapOrStructValue(v.value, "map")
|
||||
if err != nil {
|
||||
return v.error(err)
|
||||
}
|
||||
|
||||
if t.ElementRelationship == schema.Atomic {
|
||||
v.doLeaf()
|
||||
}
|
||||
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
errs = v.visitMapItems(t, m)
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v validatingObjectWalker) doUntyped(t schema.Untyped) (errs ValidationErrors) {
|
||||
if t.ElementRelationship == "" || t.ElementRelationship == schema.Atomic {
|
||||
// Untyped sections allow anything, and are considered leaf
|
||||
// fields.
|
||||
v.doLeaf()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
21
vendor/sigs.k8s.io/structured-merge-diff/value/doc.go
generated
vendored
Normal file
21
vendor/sigs.k8s.io/structured-merge-diff/value/doc.go
generated
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 value defines types for an in-memory representation of yaml or json
|
||||
// objects, organized for convenient comparison with a schema (as defined by
|
||||
// the sibling schema package). Functions for reading and writing the objects
|
||||
// are also provided.
|
||||
package value
|
||||
234
vendor/sigs.k8s.io/structured-merge-diff/value/unstructured.go
generated
vendored
Normal file
234
vendor/sigs.k8s.io/structured-merge-diff/value/unstructured.go
generated
vendored
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 value
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// FromYAML is a helper function for reading a YAML document; it attempts to
|
||||
// preserve order of keys within maps/structs. This is as a convenience to
|
||||
// humans keeping YAML documents, not because there is a behavior difference.
|
||||
//
|
||||
// Known bug: objects with top-level arrays don't parse correctly.
|
||||
func FromYAML(input []byte) (Value, error) {
|
||||
var decoded interface{}
|
||||
|
||||
if len(input) == 4 && string(input) == "null" {
|
||||
// Special case since the yaml package doesn't accurately
|
||||
// preserve this.
|
||||
return Value{Null: true}, nil
|
||||
}
|
||||
|
||||
// This attempts to enable order sensitivity; note the yaml package is
|
||||
// broken for documents that have root-level arrays, hence the two-step
|
||||
// approach. TODO: This is a horrific hack. Is it worth it?
|
||||
var ms yaml.MapSlice
|
||||
if err := yaml.Unmarshal(input, &ms); err == nil {
|
||||
decoded = ms
|
||||
} else if err := yaml.Unmarshal(input, &decoded); err != nil {
|
||||
return Value{}, err
|
||||
}
|
||||
|
||||
v, err := FromUnstructured(decoded)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("failed to interpret (%v):\n%s", err, input)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// FromJSON is a helper function for reading a JSON document
|
||||
func FromJSON(input []byte) (Value, error) {
|
||||
var decoded interface{}
|
||||
|
||||
if err := json.Unmarshal(input, &decoded); err != nil {
|
||||
return Value{}, err
|
||||
}
|
||||
|
||||
v, err := FromUnstructured(decoded)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("failed to interpret (%v):\n%s", err, input)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// FromUnstructured will convert a go interface to a Value.
|
||||
// It's most commonly expected to be used with map[string]interface{} as the
|
||||
// input. `in` must not have any structures with cycles in them.
|
||||
// yaml.MapSlice may be used for order-preservation.
|
||||
func FromUnstructured(in interface{}) (Value, error) {
|
||||
if in == nil {
|
||||
return Value{Null: true}, nil
|
||||
}
|
||||
switch t := in.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
m := Map{}
|
||||
for rawKey, rawVal := range t {
|
||||
k, ok := rawKey.(string)
|
||||
if !ok {
|
||||
return Value{}, fmt.Errorf("key %#v: not a string", k)
|
||||
}
|
||||
v, err := FromUnstructured(rawVal)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("key %v: %v", k, err)
|
||||
}
|
||||
m.Set(k, v)
|
||||
}
|
||||
return Value{MapValue: &m}, nil
|
||||
case map[string]interface{}:
|
||||
m := Map{}
|
||||
for k, rawVal := range t {
|
||||
v, err := FromUnstructured(rawVal)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("key %v: %v", k, err)
|
||||
}
|
||||
m.Set(k, v)
|
||||
}
|
||||
return Value{MapValue: &m}, nil
|
||||
case yaml.MapSlice:
|
||||
m := Map{}
|
||||
for _, item := range t {
|
||||
k, ok := item.Key.(string)
|
||||
if !ok {
|
||||
return Value{}, fmt.Errorf("key %#v is not a string", item.Key)
|
||||
}
|
||||
v, err := FromUnstructured(item.Value)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("key %v: %v", k, err)
|
||||
}
|
||||
m.Set(k, v)
|
||||
}
|
||||
return Value{MapValue: &m}, nil
|
||||
case []interface{}:
|
||||
l := List{}
|
||||
for i, rawVal := range t {
|
||||
v, err := FromUnstructured(rawVal)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("index %v: %v", i, err)
|
||||
}
|
||||
l.Items = append(l.Items, v)
|
||||
}
|
||||
return Value{ListValue: &l}, nil
|
||||
case int:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case int8:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case int16:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case int32:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case int64:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case uint:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case uint8:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case uint16:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case uint32:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case float32:
|
||||
f := Float(t)
|
||||
return Value{FloatValue: &f}, nil
|
||||
case float64:
|
||||
f := Float(t)
|
||||
return Value{FloatValue: &f}, nil
|
||||
case string:
|
||||
return StringValue(t), nil
|
||||
case bool:
|
||||
return BooleanValue(t), nil
|
||||
default:
|
||||
return Value{}, fmt.Errorf("type unimplemented: %t", in)
|
||||
}
|
||||
}
|
||||
|
||||
// ToYAML is a helper function for producing a YAML document; it attempts to
|
||||
// preserve order of keys within maps/structs. This is as a convenience to
|
||||
// humans keeping YAML documents, not because there is a behavior difference.
|
||||
func (v *Value) ToYAML() ([]byte, error) {
|
||||
return yaml.Marshal(v.ToUnstructured(true))
|
||||
}
|
||||
|
||||
// ToJSON is a helper function for producing a JSon document.
|
||||
func (v *Value) ToJSON() ([]byte, error) {
|
||||
return json.Marshal(v.ToUnstructured(false))
|
||||
}
|
||||
|
||||
// ToUnstructured will convert the Value into a go-typed object.
|
||||
// If preserveOrder is true, then maps will be converted to the yaml.MapSlice
|
||||
// type. Otherwise, map[string]interface{} must be used-- this destroys
|
||||
// ordering information and is not recommended if the result of this will be
|
||||
// serialized. Other types:
|
||||
// * list -> []interface{}
|
||||
// * others -> corresponding go type, wrapped in an interface{}
|
||||
//
|
||||
// Of note, floats and ints will always come out as float64 and int64,
|
||||
// respectively.
|
||||
func (v *Value) ToUnstructured(preserveOrder bool) interface{} {
|
||||
switch {
|
||||
case v.FloatValue != nil:
|
||||
f := float64(*v.FloatValue)
|
||||
return f
|
||||
case v.IntValue != nil:
|
||||
i := int64(*v.IntValue)
|
||||
return i
|
||||
case v.StringValue != nil:
|
||||
return string(*v.StringValue)
|
||||
case v.BooleanValue != nil:
|
||||
return bool(*v.BooleanValue)
|
||||
case v.ListValue != nil:
|
||||
out := []interface{}{}
|
||||
for _, item := range v.ListValue.Items {
|
||||
out = append(out, item.ToUnstructured(preserveOrder))
|
||||
}
|
||||
return out
|
||||
case v.MapValue != nil:
|
||||
m := v.MapValue
|
||||
if preserveOrder {
|
||||
ms := make(yaml.MapSlice, len(m.Items))
|
||||
for i := range m.Items {
|
||||
ms[i] = yaml.MapItem{
|
||||
Key: m.Items[i].Name,
|
||||
Value: m.Items[i].Value.ToUnstructured(preserveOrder),
|
||||
}
|
||||
}
|
||||
return ms
|
||||
}
|
||||
// This case is unavoidably lossy.
|
||||
out := map[string]interface{}{}
|
||||
for i := range m.Items {
|
||||
out[m.Items[i].Name] = m.Items[i].Value.ToUnstructured(preserveOrder)
|
||||
}
|
||||
return out
|
||||
default:
|
||||
fallthrough
|
||||
case v.Null == true:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
139
vendor/sigs.k8s.io/structured-merge-diff/value/value.go
generated
vendored
Normal file
139
vendor/sigs.k8s.io/structured-merge-diff/value/value.go
generated
vendored
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
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 value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A Value is an object; it corresponds to an 'atom' in the schema.
|
||||
type Value struct {
|
||||
// Exactly one of the below must be set.
|
||||
FloatValue *Float
|
||||
IntValue *Int
|
||||
StringValue *String
|
||||
BooleanValue *Boolean
|
||||
ListValue *List
|
||||
MapValue *Map
|
||||
Null bool // represents an explicit `"foo" = null`
|
||||
}
|
||||
|
||||
type Int int64
|
||||
type Float float64
|
||||
type String string
|
||||
type Boolean bool
|
||||
|
||||
// Field is an individual key-value pair.
|
||||
type Field struct {
|
||||
Name string
|
||||
Value Value
|
||||
}
|
||||
|
||||
// List is a list of items.
|
||||
type List struct {
|
||||
Items []Value
|
||||
}
|
||||
|
||||
// Map is a map of key-value pairs. It represents both structs and maps. We use
|
||||
// a list and a go-language map to preserve order.
|
||||
//
|
||||
// Set and Get helpers are provided.
|
||||
type Map struct {
|
||||
Items []Field
|
||||
|
||||
// may be nil; lazily constructed.
|
||||
// TODO: Direct modifications to Items above will cause serious problems.
|
||||
index map[string]*Field
|
||||
}
|
||||
|
||||
// Get returns the (Field, true) or (nil, false) if it is not present
|
||||
func (m *Map) Get(key string) (*Field, bool) {
|
||||
if m.index == nil {
|
||||
m.index = map[string]*Field{}
|
||||
for i := range m.Items {
|
||||
f := &m.Items[i]
|
||||
m.index[f.Name] = f
|
||||
}
|
||||
}
|
||||
f, ok := m.index[key]
|
||||
return f, ok
|
||||
}
|
||||
|
||||
// Set inserts or updates the given item.
|
||||
func (m *Map) Set(key string, value Value) {
|
||||
if f, ok := m.Get(key); ok {
|
||||
f.Value = value
|
||||
return
|
||||
}
|
||||
m.Items = append(m.Items, Field{Name: key, Value: value})
|
||||
m.index = nil // Since the append might have reallocated
|
||||
}
|
||||
|
||||
// StringValue returns s as a scalar string Value.
|
||||
func StringValue(s string) Value {
|
||||
s2 := String(s)
|
||||
return Value{StringValue: &s2}
|
||||
}
|
||||
|
||||
// IntValue returns i as a scalar numeric (integer) Value.
|
||||
func IntValue(i int) Value {
|
||||
i2 := Int(i)
|
||||
return Value{IntValue: &i2}
|
||||
}
|
||||
|
||||
// FloatValue returns f as a scalar numeric (float) Value.
|
||||
func FloatValue(f float64) Value {
|
||||
f2 := Float(f)
|
||||
return Value{FloatValue: &f2}
|
||||
}
|
||||
|
||||
// BooleanValue returns b as a scalar boolean Value.
|
||||
func BooleanValue(b bool) Value {
|
||||
b2 := Boolean(b)
|
||||
return Value{BooleanValue: &b2}
|
||||
}
|
||||
|
||||
// String returns a human-readable representation of the value.
|
||||
func (v Value) String() string {
|
||||
switch {
|
||||
case v.FloatValue != nil:
|
||||
return fmt.Sprintf("%v", *v.FloatValue)
|
||||
case v.IntValue != nil:
|
||||
return fmt.Sprintf("%v", *v.IntValue)
|
||||
case v.StringValue != nil:
|
||||
return fmt.Sprintf("%q", *v.StringValue)
|
||||
case v.BooleanValue != nil:
|
||||
return fmt.Sprintf("%v", *v.BooleanValue)
|
||||
case v.ListValue != nil:
|
||||
strs := []string{}
|
||||
for _, item := range v.ListValue.Items {
|
||||
strs = append(strs, item.String())
|
||||
}
|
||||
return "[" + strings.Join(strs, ",") + "]"
|
||||
case v.MapValue != nil:
|
||||
strs := []string{}
|
||||
for _, i := range v.MapValue.Items {
|
||||
strs = append(strs, fmt.Sprintf("%v=%v", i.Name, i.Value))
|
||||
}
|
||||
return "{" + strings.Join(strs, ";") + "}"
|
||||
default:
|
||||
fallthrough
|
||||
case v.Null == true:
|
||||
return "null"
|
||||
}
|
||||
}
|
||||
20
vendor/sigs.k8s.io/yaml/.gitignore
generated
vendored
Normal file
20
vendor/sigs.k8s.io/yaml/.gitignore
generated
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# OSX leaves these everywhere on SMB shares
|
||||
._*
|
||||
|
||||
# Eclipse files
|
||||
.classpath
|
||||
.project
|
||||
.settings/**
|
||||
|
||||
# Emacs save files
|
||||
*~
|
||||
|
||||
# Vim-related files
|
||||
[._]*.s[a-w][a-z]
|
||||
[._]s[a-w][a-z]
|
||||
*.un~
|
||||
Session.vim
|
||||
.netrwhist
|
||||
|
||||
# Go test binaries
|
||||
*.test
|
||||
14
vendor/sigs.k8s.io/yaml/.travis.yml
generated
vendored
Normal file
14
vendor/sigs.k8s.io/yaml/.travis.yml
generated
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
language: go
|
||||
dist: xenial
|
||||
go:
|
||||
- 1.9.x
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
script:
|
||||
- go get -t -v ./...
|
||||
- diff -u <(echo -n) <(gofmt -d .)
|
||||
- diff -u <(echo -n) <(golint $(go list -e ./...) | grep -v YAMLToJSON)
|
||||
- go tool vet .
|
||||
- go test -v -race ./...
|
||||
install:
|
||||
- go get golang.org/x/lint/golint
|
||||
31
vendor/sigs.k8s.io/yaml/CONTRIBUTING.md
generated
vendored
Normal file
31
vendor/sigs.k8s.io/yaml/CONTRIBUTING.md
generated
vendored
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# Contributing Guidelines
|
||||
|
||||
Welcome to Kubernetes. We are excited about the prospect of you joining our [community](https://github.com/kubernetes/community)! The Kubernetes community abides by the CNCF [code of conduct](code-of-conduct.md). Here is an excerpt:
|
||||
|
||||
_As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities._
|
||||
|
||||
## Getting Started
|
||||
|
||||
We have full documentation on how to get started contributing here:
|
||||
|
||||
<!---
|
||||
If your repo has certain guidelines for contribution, put them here ahead of the general k8s resources
|
||||
-->
|
||||
|
||||
- [Contributor License Agreement](https://git.k8s.io/community/CLA.md) Kubernetes projects require that you sign a Contributor License Agreement (CLA) before we can accept your pull requests
|
||||
- [Kubernetes Contributor Guide](http://git.k8s.io/community/contributors/guide) - Main contributor documentation, or you can just jump directly to the [contributing section](http://git.k8s.io/community/contributors/guide#contributing)
|
||||
- [Contributor Cheat Sheet](https://git.k8s.io/community/contributors/guide/contributor-cheatsheet.md) - Common resources for existing developers
|
||||
|
||||
## Mentorship
|
||||
|
||||
- [Mentoring Initiatives](https://git.k8s.io/community/mentoring) - We have a diverse set of mentorship programs available that are always looking for volunteers!
|
||||
|
||||
<!---
|
||||
Custom Information - if you're copying this template for the first time you can add custom content here, for example:
|
||||
|
||||
## Contact Information
|
||||
|
||||
- [Slack channel](https://kubernetes.slack.com/messages/kubernetes-users) - Replace `kubernetes-users` with your slack channel string, this will send users directly to your channel.
|
||||
- [Mailing list](URL)
|
||||
|
||||
-->
|
||||
50
vendor/sigs.k8s.io/yaml/LICENSE
generated
vendored
Normal file
50
vendor/sigs.k8s.io/yaml/LICENSE
generated
vendored
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Sam Ghods
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
|
||||
Copyright (c) 2012 The Go Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
25
vendor/sigs.k8s.io/yaml/OWNERS
generated
vendored
Normal file
25
vendor/sigs.k8s.io/yaml/OWNERS
generated
vendored
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
approvers:
|
||||
- dims
|
||||
- lavalamp
|
||||
- smarterclayton
|
||||
- deads2k
|
||||
- sttts
|
||||
- liggitt
|
||||
- caesarxuchao
|
||||
reviewers:
|
||||
- dims
|
||||
- thockin
|
||||
- lavalamp
|
||||
- smarterclayton
|
||||
- wojtek-t
|
||||
- deads2k
|
||||
- derekwaynecarr
|
||||
- caesarxuchao
|
||||
- mikedanese
|
||||
- liggitt
|
||||
- gmarek
|
||||
- sttts
|
||||
- ncdc
|
||||
- tallclair
|
||||
labels:
|
||||
- sig/api-machinery
|
||||
121
vendor/sigs.k8s.io/yaml/README.md
generated
vendored
Normal file
121
vendor/sigs.k8s.io/yaml/README.md
generated
vendored
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
# YAML marshaling and unmarshaling support for Go
|
||||
|
||||
[](https://travis-ci.org/ghodss/yaml)
|
||||
|
||||
## Introduction
|
||||
|
||||
A wrapper around [go-yaml](https://github.com/go-yaml/yaml) designed to enable a better way of handling YAML when marshaling to and from structs.
|
||||
|
||||
In short, this library first converts YAML to JSON using go-yaml and then uses `json.Marshal` and `json.Unmarshal` to convert to or from the struct. This means that it effectively reuses the JSON struct tags as well as the custom JSON methods `MarshalJSON` and `UnmarshalJSON` unlike go-yaml. For a detailed overview of the rationale behind this method, [see this blog post](http://ghodss.com/2014/the-right-way-to-handle-yaml-in-golang/).
|
||||
|
||||
## Compatibility
|
||||
|
||||
This package uses [go-yaml](https://github.com/go-yaml/yaml) and therefore supports [everything go-yaml supports](https://github.com/go-yaml/yaml#compatibility).
|
||||
|
||||
## Caveats
|
||||
|
||||
**Caveat #1:** When using `yaml.Marshal` and `yaml.Unmarshal`, binary data should NOT be preceded with the `!!binary` YAML tag. If you do, go-yaml will convert the binary data from base64 to native binary data, which is not compatible with JSON. You can still use binary in your YAML files though - just store them without the `!!binary` tag and decode the base64 in your code (e.g. in the custom JSON methods `MarshalJSON` and `UnmarshalJSON`). This also has the benefit that your YAML and your JSON binary data will be decoded exactly the same way. As an example:
|
||||
|
||||
```
|
||||
BAD:
|
||||
exampleKey: !!binary gIGC
|
||||
|
||||
GOOD:
|
||||
exampleKey: gIGC
|
||||
... and decode the base64 data in your code.
|
||||
```
|
||||
|
||||
**Caveat #2:** When using `YAMLToJSON` directly, maps with keys that are maps will result in an error since this is not supported by JSON. This error will occur in `Unmarshal` as well since you can't unmarshal map keys anyways since struct fields can't be keys.
|
||||
|
||||
## Installation and usage
|
||||
|
||||
To install, run:
|
||||
|
||||
```
|
||||
$ go get github.com/ghodss/yaml
|
||||
```
|
||||
|
||||
And import using:
|
||||
|
||||
```
|
||||
import "github.com/ghodss/yaml"
|
||||
```
|
||||
|
||||
Usage is very similar to the JSON library:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
Name string `json:"name"` // Affects YAML field names too.
|
||||
Age int `json:"age"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Marshal a Person struct to YAML.
|
||||
p := Person{"John", 30}
|
||||
y, err := yaml.Marshal(p)
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(y))
|
||||
/* Output:
|
||||
age: 30
|
||||
name: John
|
||||
*/
|
||||
|
||||
// Unmarshal the YAML back into a Person struct.
|
||||
var p2 Person
|
||||
err = yaml.Unmarshal(y, &p2)
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(p2)
|
||||
/* Output:
|
||||
{John 30}
|
||||
*/
|
||||
}
|
||||
```
|
||||
|
||||
`yaml.YAMLToJSON` and `yaml.JSONToYAML` methods are also available:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
)
|
||||
|
||||
func main() {
|
||||
j := []byte(`{"name": "John", "age": 30}`)
|
||||
y, err := yaml.JSONToYAML(j)
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(y))
|
||||
/* Output:
|
||||
name: John
|
||||
age: 30
|
||||
*/
|
||||
j2, err := yaml.YAMLToJSON(y)
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(j2))
|
||||
/* Output:
|
||||
{"age":30,"name":"John"}
|
||||
*/
|
||||
}
|
||||
```
|
||||
9
vendor/sigs.k8s.io/yaml/RELEASE.md
generated
vendored
Normal file
9
vendor/sigs.k8s.io/yaml/RELEASE.md
generated
vendored
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# Release Process
|
||||
|
||||
The `yaml` Project is released on an as-needed basis. The process is as follows:
|
||||
|
||||
1. An issue is proposing a new release with a changelog since the last release
|
||||
1. All [OWNERS](OWNERS) must LGTM this release
|
||||
1. An OWNER runs `git tag -s $VERSION` and inserts the changelog and pushes the tag with `git push $VERSION`
|
||||
1. The release issue is closed
|
||||
1. An announcement email is sent to `kubernetes-dev@googlegroups.com` with the subject `[ANNOUNCE] kubernetes-template-project $VERSION is released`
|
||||
17
vendor/sigs.k8s.io/yaml/SECURITY_CONTACTS
generated
vendored
Normal file
17
vendor/sigs.k8s.io/yaml/SECURITY_CONTACTS
generated
vendored
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# Defined below are the security contacts for this repo.
|
||||
#
|
||||
# They are the contact point for the Product Security Team to reach out
|
||||
# to for triaging and handling of incoming issues.
|
||||
#
|
||||
# The below names agree to abide by the
|
||||
# [Embargo Policy](https://github.com/kubernetes/sig-release/blob/master/security-release-process-documentation/security-release-process.md#embargo-policy)
|
||||
# and will be removed and replaced if they violate that agreement.
|
||||
#
|
||||
# DO NOT REPORT SECURITY VULNERABILITIES DIRECTLY TO THESE NAMES, FOLLOW THE
|
||||
# INSTRUCTIONS AT https://kubernetes.io/security/
|
||||
|
||||
cjcullen
|
||||
jessfraz
|
||||
liggitt
|
||||
philips
|
||||
tallclair
|
||||
3
vendor/sigs.k8s.io/yaml/code-of-conduct.md
generated
vendored
Normal file
3
vendor/sigs.k8s.io/yaml/code-of-conduct.md
generated
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Kubernetes Community Code of Conduct
|
||||
|
||||
Please refer to our [Kubernetes Community Code of Conduct](https://git.k8s.io/community/code-of-conduct.md)
|
||||
502
vendor/sigs.k8s.io/yaml/fields.go
generated
vendored
Normal file
502
vendor/sigs.k8s.io/yaml/fields.go
generated
vendored
Normal file
|
|
@ -0,0 +1,502 @@
|
|||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// indirect walks down v allocating pointers as needed,
|
||||
// until it gets to a non-pointer.
|
||||
// if it encounters an Unmarshaler, indirect stops and returns that.
|
||||
// if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
|
||||
func indirect(v reflect.Value, decodingNull bool) (json.Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
|
||||
// If v is a named type and is addressable,
|
||||
// start with its address, so that if the type has pointer methods,
|
||||
// we find them.
|
||||
if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
|
||||
v = v.Addr()
|
||||
}
|
||||
for {
|
||||
// Load value from interface, but only if the result will be
|
||||
// usefully addressable.
|
||||
if v.Kind() == reflect.Interface && !v.IsNil() {
|
||||
e := v.Elem()
|
||||
if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
|
||||
v = e
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if v.Kind() != reflect.Ptr {
|
||||
break
|
||||
}
|
||||
|
||||
if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
|
||||
break
|
||||
}
|
||||
if v.IsNil() {
|
||||
if v.CanSet() {
|
||||
v.Set(reflect.New(v.Type().Elem()))
|
||||
} else {
|
||||
v = reflect.New(v.Type().Elem())
|
||||
}
|
||||
}
|
||||
if v.Type().NumMethod() > 0 {
|
||||
if u, ok := v.Interface().(json.Unmarshaler); ok {
|
||||
return u, nil, reflect.Value{}
|
||||
}
|
||||
if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
|
||||
return nil, u, reflect.Value{}
|
||||
}
|
||||
}
|
||||
v = v.Elem()
|
||||
}
|
||||
return nil, nil, v
|
||||
}
|
||||
|
||||
// A field represents a single field found in a struct.
|
||||
type field struct {
|
||||
name string
|
||||
nameBytes []byte // []byte(name)
|
||||
equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
|
||||
|
||||
tag bool
|
||||
index []int
|
||||
typ reflect.Type
|
||||
omitEmpty bool
|
||||
quoted bool
|
||||
}
|
||||
|
||||
func fillField(f field) field {
|
||||
f.nameBytes = []byte(f.name)
|
||||
f.equalFold = foldFunc(f.nameBytes)
|
||||
return f
|
||||
}
|
||||
|
||||
// byName sorts field by name, breaking ties with depth,
|
||||
// then breaking ties with "name came from json tag", then
|
||||
// breaking ties with index sequence.
|
||||
type byName []field
|
||||
|
||||
func (x byName) Len() int { return len(x) }
|
||||
|
||||
func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||
|
||||
func (x byName) Less(i, j int) bool {
|
||||
if x[i].name != x[j].name {
|
||||
return x[i].name < x[j].name
|
||||
}
|
||||
if len(x[i].index) != len(x[j].index) {
|
||||
return len(x[i].index) < len(x[j].index)
|
||||
}
|
||||
if x[i].tag != x[j].tag {
|
||||
return x[i].tag
|
||||
}
|
||||
return byIndex(x).Less(i, j)
|
||||
}
|
||||
|
||||
// byIndex sorts field by index sequence.
|
||||
type byIndex []field
|
||||
|
||||
func (x byIndex) Len() int { return len(x) }
|
||||
|
||||
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||
|
||||
func (x byIndex) Less(i, j int) bool {
|
||||
for k, xik := range x[i].index {
|
||||
if k >= len(x[j].index) {
|
||||
return false
|
||||
}
|
||||
if xik != x[j].index[k] {
|
||||
return xik < x[j].index[k]
|
||||
}
|
||||
}
|
||||
return len(x[i].index) < len(x[j].index)
|
||||
}
|
||||
|
||||
// typeFields returns a list of fields that JSON should recognize for the given type.
|
||||
// The algorithm is breadth-first search over the set of structs to include - the top struct
|
||||
// and then any reachable anonymous structs.
|
||||
func typeFields(t reflect.Type) []field {
|
||||
// Anonymous fields to explore at the current level and the next.
|
||||
current := []field{}
|
||||
next := []field{{typ: t}}
|
||||
|
||||
// Count of queued names for current level and the next.
|
||||
count := map[reflect.Type]int{}
|
||||
nextCount := map[reflect.Type]int{}
|
||||
|
||||
// Types already visited at an earlier level.
|
||||
visited := map[reflect.Type]bool{}
|
||||
|
||||
// Fields found.
|
||||
var fields []field
|
||||
|
||||
for len(next) > 0 {
|
||||
current, next = next, current[:0]
|
||||
count, nextCount = nextCount, map[reflect.Type]int{}
|
||||
|
||||
for _, f := range current {
|
||||
if visited[f.typ] {
|
||||
continue
|
||||
}
|
||||
visited[f.typ] = true
|
||||
|
||||
// Scan f.typ for fields to include.
|
||||
for i := 0; i < f.typ.NumField(); i++ {
|
||||
sf := f.typ.Field(i)
|
||||
if sf.PkgPath != "" { // unexported
|
||||
continue
|
||||
}
|
||||
tag := sf.Tag.Get("json")
|
||||
if tag == "-" {
|
||||
continue
|
||||
}
|
||||
name, opts := parseTag(tag)
|
||||
if !isValidTag(name) {
|
||||
name = ""
|
||||
}
|
||||
index := make([]int, len(f.index)+1)
|
||||
copy(index, f.index)
|
||||
index[len(f.index)] = i
|
||||
|
||||
ft := sf.Type
|
||||
if ft.Name() == "" && ft.Kind() == reflect.Ptr {
|
||||
// Follow pointer.
|
||||
ft = ft.Elem()
|
||||
}
|
||||
|
||||
// Record found field and index sequence.
|
||||
if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
|
||||
tagged := name != ""
|
||||
if name == "" {
|
||||
name = sf.Name
|
||||
}
|
||||
fields = append(fields, fillField(field{
|
||||
name: name,
|
||||
tag: tagged,
|
||||
index: index,
|
||||
typ: ft,
|
||||
omitEmpty: opts.Contains("omitempty"),
|
||||
quoted: opts.Contains("string"),
|
||||
}))
|
||||
if count[f.typ] > 1 {
|
||||
// If there were multiple instances, add a second,
|
||||
// so that the annihilation code will see a duplicate.
|
||||
// It only cares about the distinction between 1 or 2,
|
||||
// so don't bother generating any more copies.
|
||||
fields = append(fields, fields[len(fields)-1])
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Record new anonymous struct to explore in next round.
|
||||
nextCount[ft]++
|
||||
if nextCount[ft] == 1 {
|
||||
next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft}))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Sort(byName(fields))
|
||||
|
||||
// Delete all fields that are hidden by the Go rules for embedded fields,
|
||||
// except that fields with JSON tags are promoted.
|
||||
|
||||
// The fields are sorted in primary order of name, secondary order
|
||||
// of field index length. Loop over names; for each name, delete
|
||||
// hidden fields by choosing the one dominant field that survives.
|
||||
out := fields[:0]
|
||||
for advance, i := 0, 0; i < len(fields); i += advance {
|
||||
// One iteration per name.
|
||||
// Find the sequence of fields with the name of this first field.
|
||||
fi := fields[i]
|
||||
name := fi.name
|
||||
for advance = 1; i+advance < len(fields); advance++ {
|
||||
fj := fields[i+advance]
|
||||
if fj.name != name {
|
||||
break
|
||||
}
|
||||
}
|
||||
if advance == 1 { // Only one field with this name
|
||||
out = append(out, fi)
|
||||
continue
|
||||
}
|
||||
dominant, ok := dominantField(fields[i : i+advance])
|
||||
if ok {
|
||||
out = append(out, dominant)
|
||||
}
|
||||
}
|
||||
|
||||
fields = out
|
||||
sort.Sort(byIndex(fields))
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
// dominantField looks through the fields, all of which are known to
|
||||
// have the same name, to find the single field that dominates the
|
||||
// others using Go's embedding rules, modified by the presence of
|
||||
// JSON tags. If there are multiple top-level fields, the boolean
|
||||
// will be false: This condition is an error in Go and we skip all
|
||||
// the fields.
|
||||
func dominantField(fields []field) (field, bool) {
|
||||
// The fields are sorted in increasing index-length order. The winner
|
||||
// must therefore be one with the shortest index length. Drop all
|
||||
// longer entries, which is easy: just truncate the slice.
|
||||
length := len(fields[0].index)
|
||||
tagged := -1 // Index of first tagged field.
|
||||
for i, f := range fields {
|
||||
if len(f.index) > length {
|
||||
fields = fields[:i]
|
||||
break
|
||||
}
|
||||
if f.tag {
|
||||
if tagged >= 0 {
|
||||
// Multiple tagged fields at the same level: conflict.
|
||||
// Return no field.
|
||||
return field{}, false
|
||||
}
|
||||
tagged = i
|
||||
}
|
||||
}
|
||||
if tagged >= 0 {
|
||||
return fields[tagged], true
|
||||
}
|
||||
// All remaining fields have the same length. If there's more than one,
|
||||
// we have a conflict (two fields named "X" at the same level) and we
|
||||
// return no field.
|
||||
if len(fields) > 1 {
|
||||
return field{}, false
|
||||
}
|
||||
return fields[0], true
|
||||
}
|
||||
|
||||
var fieldCache struct {
|
||||
sync.RWMutex
|
||||
m map[reflect.Type][]field
|
||||
}
|
||||
|
||||
// cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
|
||||
func cachedTypeFields(t reflect.Type) []field {
|
||||
fieldCache.RLock()
|
||||
f := fieldCache.m[t]
|
||||
fieldCache.RUnlock()
|
||||
if f != nil {
|
||||
return f
|
||||
}
|
||||
|
||||
// Compute fields without lock.
|
||||
// Might duplicate effort but won't hold other computations back.
|
||||
f = typeFields(t)
|
||||
if f == nil {
|
||||
f = []field{}
|
||||
}
|
||||
|
||||
fieldCache.Lock()
|
||||
if fieldCache.m == nil {
|
||||
fieldCache.m = map[reflect.Type][]field{}
|
||||
}
|
||||
fieldCache.m[t] = f
|
||||
fieldCache.Unlock()
|
||||
return f
|
||||
}
|
||||
|
||||
func isValidTag(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
for _, c := range s {
|
||||
switch {
|
||||
case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
|
||||
// Backslash and quote chars are reserved, but
|
||||
// otherwise any punctuation chars are allowed
|
||||
// in a tag name.
|
||||
default:
|
||||
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
const (
|
||||
caseMask = ^byte(0x20) // Mask to ignore case in ASCII.
|
||||
kelvin = '\u212a'
|
||||
smallLongEss = '\u017f'
|
||||
)
|
||||
|
||||
// foldFunc returns one of four different case folding equivalence
|
||||
// functions, from most general (and slow) to fastest:
|
||||
//
|
||||
// 1) bytes.EqualFold, if the key s contains any non-ASCII UTF-8
|
||||
// 2) equalFoldRight, if s contains special folding ASCII ('k', 'K', 's', 'S')
|
||||
// 3) asciiEqualFold, no special, but includes non-letters (including _)
|
||||
// 4) simpleLetterEqualFold, no specials, no non-letters.
|
||||
//
|
||||
// The letters S and K are special because they map to 3 runes, not just 2:
|
||||
// * S maps to s and to U+017F 'ſ' Latin small letter long s
|
||||
// * k maps to K and to U+212A 'K' Kelvin sign
|
||||
// See http://play.golang.org/p/tTxjOc0OGo
|
||||
//
|
||||
// The returned function is specialized for matching against s and
|
||||
// should only be given s. It's not curried for performance reasons.
|
||||
func foldFunc(s []byte) func(s, t []byte) bool {
|
||||
nonLetter := false
|
||||
special := false // special letter
|
||||
for _, b := range s {
|
||||
if b >= utf8.RuneSelf {
|
||||
return bytes.EqualFold
|
||||
}
|
||||
upper := b & caseMask
|
||||
if upper < 'A' || upper > 'Z' {
|
||||
nonLetter = true
|
||||
} else if upper == 'K' || upper == 'S' {
|
||||
// See above for why these letters are special.
|
||||
special = true
|
||||
}
|
||||
}
|
||||
if special {
|
||||
return equalFoldRight
|
||||
}
|
||||
if nonLetter {
|
||||
return asciiEqualFold
|
||||
}
|
||||
return simpleLetterEqualFold
|
||||
}
|
||||
|
||||
// equalFoldRight is a specialization of bytes.EqualFold when s is
|
||||
// known to be all ASCII (including punctuation), but contains an 's',
|
||||
// 'S', 'k', or 'K', requiring a Unicode fold on the bytes in t.
|
||||
// See comments on foldFunc.
|
||||
func equalFoldRight(s, t []byte) bool {
|
||||
for _, sb := range s {
|
||||
if len(t) == 0 {
|
||||
return false
|
||||
}
|
||||
tb := t[0]
|
||||
if tb < utf8.RuneSelf {
|
||||
if sb != tb {
|
||||
sbUpper := sb & caseMask
|
||||
if 'A' <= sbUpper && sbUpper <= 'Z' {
|
||||
if sbUpper != tb&caseMask {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
t = t[1:]
|
||||
continue
|
||||
}
|
||||
// sb is ASCII and t is not. t must be either kelvin
|
||||
// sign or long s; sb must be s, S, k, or K.
|
||||
tr, size := utf8.DecodeRune(t)
|
||||
switch sb {
|
||||
case 's', 'S':
|
||||
if tr != smallLongEss {
|
||||
return false
|
||||
}
|
||||
case 'k', 'K':
|
||||
if tr != kelvin {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
t = t[size:]
|
||||
|
||||
}
|
||||
if len(t) > 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// asciiEqualFold is a specialization of bytes.EqualFold for use when
|
||||
// s is all ASCII (but may contain non-letters) and contains no
|
||||
// special-folding letters.
|
||||
// See comments on foldFunc.
|
||||
func asciiEqualFold(s, t []byte) bool {
|
||||
if len(s) != len(t) {
|
||||
return false
|
||||
}
|
||||
for i, sb := range s {
|
||||
tb := t[i]
|
||||
if sb == tb {
|
||||
continue
|
||||
}
|
||||
if ('a' <= sb && sb <= 'z') || ('A' <= sb && sb <= 'Z') {
|
||||
if sb&caseMask != tb&caseMask {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// simpleLetterEqualFold is a specialization of bytes.EqualFold for
|
||||
// use when s is all ASCII letters (no underscores, etc) and also
|
||||
// doesn't contain 'k', 'K', 's', or 'S'.
|
||||
// See comments on foldFunc.
|
||||
func simpleLetterEqualFold(s, t []byte) bool {
|
||||
if len(s) != len(t) {
|
||||
return false
|
||||
}
|
||||
for i, b := range s {
|
||||
if b&caseMask != t[i]&caseMask {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// tagOptions is the string following a comma in a struct field's "json"
|
||||
// tag, or the empty string. It does not include the leading comma.
|
||||
type tagOptions string
|
||||
|
||||
// parseTag splits a struct field's json tag into its name and
|
||||
// comma-separated options.
|
||||
func parseTag(tag string) (string, tagOptions) {
|
||||
if idx := strings.Index(tag, ","); idx != -1 {
|
||||
return tag[:idx], tagOptions(tag[idx+1:])
|
||||
}
|
||||
return tag, tagOptions("")
|
||||
}
|
||||
|
||||
// Contains reports whether a comma-separated list of options
|
||||
// contains a particular substr flag. substr must be surrounded by a
|
||||
// string boundary or commas.
|
||||
func (o tagOptions) Contains(optionName string) bool {
|
||||
if len(o) == 0 {
|
||||
return false
|
||||
}
|
||||
s := string(o)
|
||||
for s != "" {
|
||||
var next string
|
||||
i := strings.Index(s, ",")
|
||||
if i >= 0 {
|
||||
s, next = s[:i], s[i+1:]
|
||||
}
|
||||
if s == optionName {
|
||||
return true
|
||||
}
|
||||
s = next
|
||||
}
|
||||
return false
|
||||
}
|
||||
319
vendor/sigs.k8s.io/yaml/yaml.go
generated
vendored
Normal file
319
vendor/sigs.k8s.io/yaml/yaml.go
generated
vendored
Normal file
|
|
@ -0,0 +1,319 @@
|
|||
package yaml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Marshal marshals the object into JSON then converts JSON to YAML and returns the
|
||||
// YAML.
|
||||
func Marshal(o interface{}) ([]byte, error) {
|
||||
j, err := json.Marshal(o)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error marshaling into JSON: %v", err)
|
||||
}
|
||||
|
||||
y, err := JSONToYAML(j)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error converting JSON to YAML: %v", err)
|
||||
}
|
||||
|
||||
return y, nil
|
||||
}
|
||||
|
||||
// JSONOpt is a decoding option for decoding from JSON format.
|
||||
type JSONOpt func(*json.Decoder) *json.Decoder
|
||||
|
||||
// Unmarshal converts YAML to JSON then uses JSON to unmarshal into an object,
|
||||
// optionally configuring the behavior of the JSON unmarshal.
|
||||
func Unmarshal(y []byte, o interface{}, opts ...JSONOpt) error {
|
||||
return yamlUnmarshal(y, o, false, opts...)
|
||||
}
|
||||
|
||||
// UnmarshalStrict strictly converts YAML to JSON then uses JSON to unmarshal
|
||||
// into an object, optionally configuring the behavior of the JSON unmarshal.
|
||||
func UnmarshalStrict(y []byte, o interface{}, opts ...JSONOpt) error {
|
||||
return yamlUnmarshal(y, o, true, append(opts, DisallowUnknownFields)...)
|
||||
}
|
||||
|
||||
// yamlUnmarshal unmarshals the given YAML byte stream into the given interface,
|
||||
// optionally performing the unmarshalling strictly
|
||||
func yamlUnmarshal(y []byte, o interface{}, strict bool, opts ...JSONOpt) error {
|
||||
vo := reflect.ValueOf(o)
|
||||
unmarshalFn := yaml.Unmarshal
|
||||
if strict {
|
||||
unmarshalFn = yaml.UnmarshalStrict
|
||||
}
|
||||
j, err := yamlToJSON(y, &vo, unmarshalFn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error converting YAML to JSON: %v", err)
|
||||
}
|
||||
|
||||
err = jsonUnmarshal(bytes.NewReader(j), o, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error unmarshaling JSON: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// jsonUnmarshal unmarshals the JSON byte stream from the given reader into the
|
||||
// object, optionally applying decoder options prior to decoding. We are not
|
||||
// using json.Unmarshal directly as we want the chance to pass in non-default
|
||||
// options.
|
||||
func jsonUnmarshal(r io.Reader, o interface{}, opts ...JSONOpt) error {
|
||||
d := json.NewDecoder(r)
|
||||
for _, opt := range opts {
|
||||
d = opt(d)
|
||||
}
|
||||
if err := d.Decode(&o); err != nil {
|
||||
return fmt.Errorf("while decoding JSON: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// JSONToYAML Converts JSON to YAML.
|
||||
func JSONToYAML(j []byte) ([]byte, error) {
|
||||
// Convert the JSON to an object.
|
||||
var jsonObj interface{}
|
||||
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
|
||||
// Go JSON library doesn't try to pick the right number type (int, float,
|
||||
// etc.) when unmarshalling to interface{}, it just picks float64
|
||||
// universally. go-yaml does go through the effort of picking the right
|
||||
// number type, so we can preserve number type throughout this process.
|
||||
err := yaml.Unmarshal(j, &jsonObj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Marshal this object into YAML.
|
||||
return yaml.Marshal(jsonObj)
|
||||
}
|
||||
|
||||
// YAMLToJSON converts YAML to JSON. Since JSON is a subset of YAML,
|
||||
// passing JSON through this method should be a no-op.
|
||||
//
|
||||
// Things YAML can do that are not supported by JSON:
|
||||
// * In YAML you can have binary and null keys in your maps. These are invalid
|
||||
// in JSON. (int and float keys are converted to strings.)
|
||||
// * Binary data in YAML with the !!binary tag is not supported. If you want to
|
||||
// use binary data with this library, encode the data as base64 as usual but do
|
||||
// not use the !!binary tag in your YAML. This will ensure the original base64
|
||||
// encoded data makes it all the way through to the JSON.
|
||||
//
|
||||
// For strict decoding of YAML, use YAMLToJSONStrict.
|
||||
func YAMLToJSON(y []byte) ([]byte, error) {
|
||||
return yamlToJSON(y, nil, yaml.Unmarshal)
|
||||
}
|
||||
|
||||
// YAMLToJSONStrict is like YAMLToJSON but enables strict YAML decoding,
|
||||
// returning an error on any duplicate field names.
|
||||
func YAMLToJSONStrict(y []byte) ([]byte, error) {
|
||||
return yamlToJSON(y, nil, yaml.UnmarshalStrict)
|
||||
}
|
||||
|
||||
func yamlToJSON(y []byte, jsonTarget *reflect.Value, yamlUnmarshal func([]byte, interface{}) error) ([]byte, error) {
|
||||
// Convert the YAML to an object.
|
||||
var yamlObj interface{}
|
||||
err := yamlUnmarshal(y, &yamlObj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// YAML objects are not completely compatible with JSON objects (e.g. you
|
||||
// can have non-string keys in YAML). So, convert the YAML-compatible object
|
||||
// to a JSON-compatible object, failing with an error if irrecoverable
|
||||
// incompatibilties happen along the way.
|
||||
jsonObj, err := convertToJSONableObject(yamlObj, jsonTarget)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert this object to JSON and return the data.
|
||||
return json.Marshal(jsonObj)
|
||||
}
|
||||
|
||||
func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (interface{}, error) {
|
||||
var err error
|
||||
|
||||
// Resolve jsonTarget to a concrete value (i.e. not a pointer or an
|
||||
// interface). We pass decodingNull as false because we're not actually
|
||||
// decoding into the value, we're just checking if the ultimate target is a
|
||||
// string.
|
||||
if jsonTarget != nil {
|
||||
ju, tu, pv := indirect(*jsonTarget, false)
|
||||
// We have a JSON or Text Umarshaler at this level, so we can't be trying
|
||||
// to decode into a string.
|
||||
if ju != nil || tu != nil {
|
||||
jsonTarget = nil
|
||||
} else {
|
||||
jsonTarget = &pv
|
||||
}
|
||||
}
|
||||
|
||||
// If yamlObj is a number or a boolean, check if jsonTarget is a string -
|
||||
// if so, coerce. Else return normal.
|
||||
// If yamlObj is a map or array, find the field that each key is
|
||||
// unmarshaling to, and when you recurse pass the reflect.Value for that
|
||||
// field back into this function.
|
||||
switch typedYAMLObj := yamlObj.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
// JSON does not support arbitrary keys in a map, so we must convert
|
||||
// these keys to strings.
|
||||
//
|
||||
// From my reading of go-yaml v2 (specifically the resolve function),
|
||||
// keys can only have the types string, int, int64, float64, binary
|
||||
// (unsupported), or null (unsupported).
|
||||
strMap := make(map[string]interface{})
|
||||
for k, v := range typedYAMLObj {
|
||||
// Resolve the key to a string first.
|
||||
var keyString string
|
||||
switch typedKey := k.(type) {
|
||||
case string:
|
||||
keyString = typedKey
|
||||
case int:
|
||||
keyString = strconv.Itoa(typedKey)
|
||||
case int64:
|
||||
// go-yaml will only return an int64 as a key if the system
|
||||
// architecture is 32-bit and the key's value is between 32-bit
|
||||
// and 64-bit. Otherwise the key type will simply be int.
|
||||
keyString = strconv.FormatInt(typedKey, 10)
|
||||
case float64:
|
||||
// Stolen from go-yaml to use the same conversion to string as
|
||||
// the go-yaml library uses to convert float to string when
|
||||
// Marshaling.
|
||||
s := strconv.FormatFloat(typedKey, 'g', -1, 32)
|
||||
switch s {
|
||||
case "+Inf":
|
||||
s = ".inf"
|
||||
case "-Inf":
|
||||
s = "-.inf"
|
||||
case "NaN":
|
||||
s = ".nan"
|
||||
}
|
||||
keyString = s
|
||||
case bool:
|
||||
if typedKey {
|
||||
keyString = "true"
|
||||
} else {
|
||||
keyString = "false"
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("Unsupported map key of type: %s, key: %+#v, value: %+#v",
|
||||
reflect.TypeOf(k), k, v)
|
||||
}
|
||||
|
||||
// jsonTarget should be a struct or a map. If it's a struct, find
|
||||
// the field it's going to map to and pass its reflect.Value. If
|
||||
// it's a map, find the element type of the map and pass the
|
||||
// reflect.Value created from that type. If it's neither, just pass
|
||||
// nil - JSON conversion will error for us if it's a real issue.
|
||||
if jsonTarget != nil {
|
||||
t := *jsonTarget
|
||||
if t.Kind() == reflect.Struct {
|
||||
keyBytes := []byte(keyString)
|
||||
// Find the field that the JSON library would use.
|
||||
var f *field
|
||||
fields := cachedTypeFields(t.Type())
|
||||
for i := range fields {
|
||||
ff := &fields[i]
|
||||
if bytes.Equal(ff.nameBytes, keyBytes) {
|
||||
f = ff
|
||||
break
|
||||
}
|
||||
// Do case-insensitive comparison.
|
||||
if f == nil && ff.equalFold(ff.nameBytes, keyBytes) {
|
||||
f = ff
|
||||
}
|
||||
}
|
||||
if f != nil {
|
||||
// Find the reflect.Value of the most preferential
|
||||
// struct field.
|
||||
jtf := t.Field(f.index[0])
|
||||
strMap[keyString], err = convertToJSONableObject(v, &jtf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
} else if t.Kind() == reflect.Map {
|
||||
// Create a zero value of the map's element type to use as
|
||||
// the JSON target.
|
||||
jtv := reflect.Zero(t.Type().Elem())
|
||||
strMap[keyString], err = convertToJSONableObject(v, &jtv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
strMap[keyString], err = convertToJSONableObject(v, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return strMap, nil
|
||||
case []interface{}:
|
||||
// We need to recurse into arrays in case there are any
|
||||
// map[interface{}]interface{}'s inside and to convert any
|
||||
// numbers to strings.
|
||||
|
||||
// If jsonTarget is a slice (which it really should be), find the
|
||||
// thing it's going to map to. If it's not a slice, just pass nil
|
||||
// - JSON conversion will error for us if it's a real issue.
|
||||
var jsonSliceElemValue *reflect.Value
|
||||
if jsonTarget != nil {
|
||||
t := *jsonTarget
|
||||
if t.Kind() == reflect.Slice {
|
||||
// By default slices point to nil, but we need a reflect.Value
|
||||
// pointing to a value of the slice type, so we create one here.
|
||||
ev := reflect.Indirect(reflect.New(t.Type().Elem()))
|
||||
jsonSliceElemValue = &ev
|
||||
}
|
||||
}
|
||||
|
||||
// Make and use a new array.
|
||||
arr := make([]interface{}, len(typedYAMLObj))
|
||||
for i, v := range typedYAMLObj {
|
||||
arr[i], err = convertToJSONableObject(v, jsonSliceElemValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return arr, nil
|
||||
default:
|
||||
// If the target type is a string and the YAML type is a number,
|
||||
// convert the YAML type to a string.
|
||||
if jsonTarget != nil && (*jsonTarget).Kind() == reflect.String {
|
||||
// Based on my reading of go-yaml, it may return int, int64,
|
||||
// float64, or uint64.
|
||||
var s string
|
||||
switch typedVal := typedYAMLObj.(type) {
|
||||
case int:
|
||||
s = strconv.FormatInt(int64(typedVal), 10)
|
||||
case int64:
|
||||
s = strconv.FormatInt(typedVal, 10)
|
||||
case float64:
|
||||
s = strconv.FormatFloat(typedVal, 'g', -1, 32)
|
||||
case uint64:
|
||||
s = strconv.FormatUint(typedVal, 10)
|
||||
case bool:
|
||||
if typedVal {
|
||||
s = "true"
|
||||
} else {
|
||||
s = "false"
|
||||
}
|
||||
}
|
||||
if len(s) > 0 {
|
||||
yamlObj = interface{}(s)
|
||||
}
|
||||
}
|
||||
return yamlObj, nil
|
||||
}
|
||||
}
|
||||
14
vendor/sigs.k8s.io/yaml/yaml_go110.go
generated
vendored
Normal file
14
vendor/sigs.k8s.io/yaml/yaml_go110.go
generated
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// This file contains changes that are only compatible with go 1.10 and onwards.
|
||||
|
||||
// +build go1.10
|
||||
|
||||
package yaml
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// DisallowUnknownFields configures the JSON decoder to error out if unknown
|
||||
// fields come along, instead of dropping them by default.
|
||||
func DisallowUnknownFields(d *json.Decoder) *json.Decoder {
|
||||
d.DisallowUnknownFields()
|
||||
return d
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue