mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
bump(k8s.io/gengo):4a9ebbace691333e73f9978d798b1bad6c53a50d
This commit is contained in:
parent
b844a0722c
commit
85368d070b
22
Godeps/Godeps.json
generated
22
Godeps/Godeps.json
generated
@ -2571,39 +2571,43 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/gengo/args",
|
"ImportPath": "k8s.io/gengo/args",
|
||||||
"Rev": "9d004f4877d8dc116a678d35e43498404625599d"
|
"Rev": "4a9ebbace691333e73f9978d798b1bad6c53a50d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/gengo/examples/deepcopy-gen/generators",
|
"ImportPath": "k8s.io/gengo/examples/deepcopy-gen/generators",
|
||||||
"Rev": "9d004f4877d8dc116a678d35e43498404625599d"
|
"Rev": "4a9ebbace691333e73f9978d798b1bad6c53a50d"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/gengo/examples/defaulter-gen/generators",
|
||||||
|
"Rev": "4a9ebbace691333e73f9978d798b1bad6c53a50d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/gengo/examples/import-boss/generators",
|
"ImportPath": "k8s.io/gengo/examples/import-boss/generators",
|
||||||
"Rev": "9d004f4877d8dc116a678d35e43498404625599d"
|
"Rev": "4a9ebbace691333e73f9978d798b1bad6c53a50d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/gengo/examples/set-gen/generators",
|
"ImportPath": "k8s.io/gengo/examples/set-gen/generators",
|
||||||
"Rev": "9d004f4877d8dc116a678d35e43498404625599d"
|
"Rev": "4a9ebbace691333e73f9978d798b1bad6c53a50d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/gengo/examples/set-gen/sets",
|
"ImportPath": "k8s.io/gengo/examples/set-gen/sets",
|
||||||
"Rev": "9d004f4877d8dc116a678d35e43498404625599d"
|
"Rev": "4a9ebbace691333e73f9978d798b1bad6c53a50d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/gengo/generator",
|
"ImportPath": "k8s.io/gengo/generator",
|
||||||
"Rev": "9d004f4877d8dc116a678d35e43498404625599d"
|
"Rev": "4a9ebbace691333e73f9978d798b1bad6c53a50d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/gengo/namer",
|
"ImportPath": "k8s.io/gengo/namer",
|
||||||
"Rev": "9d004f4877d8dc116a678d35e43498404625599d"
|
"Rev": "4a9ebbace691333e73f9978d798b1bad6c53a50d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/gengo/parser",
|
"ImportPath": "k8s.io/gengo/parser",
|
||||||
"Rev": "9d004f4877d8dc116a678d35e43498404625599d"
|
"Rev": "4a9ebbace691333e73f9978d798b1bad6c53a50d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/gengo/types",
|
"ImportPath": "k8s.io/gengo/types",
|
||||||
"Rev": "9d004f4877d8dc116a678d35e43498404625599d"
|
"Rev": "4a9ebbace691333e73f9978d798b1bad6c53a50d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/heapster/metrics/api/v1/types",
|
"ImportPath": "k8s.io/heapster/metrics/api/v1/types",
|
||||||
|
210
Godeps/LICENSES
generated
210
Godeps/LICENSES
generated
@ -76197,6 +76197,216 @@ Library.
|
|||||||
================================================================================
|
================================================================================
|
||||||
|
|
||||||
|
|
||||||
|
================================================================================
|
||||||
|
= vendor/k8s.io/gengo/examples/defaulter-gen/generators licensed under: =
|
||||||
|
|
||||||
|
|
||||||
|
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 2014 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.
|
||||||
|
|
||||||
|
= vendor/k8s.io/gengo/LICENSE ad09685d909e7a9f763d2bb62d4bd6fb -
|
||||||
|
================================================================================
|
||||||
|
|
||||||
|
|
||||||
================================================================================
|
================================================================================
|
||||||
= vendor/k8s.io/gengo/examples/import-boss/generators licensed under: =
|
= vendor/k8s.io/gengo/examples/import-boss/generators licensed under: =
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@ cmd/libs/go2idl/client-gen/test_apis/testgroup.k8s.io/install
|
|||||||
cmd/libs/go2idl/client-gen/testoutput/clientset_generated/test_internalclientset/typed/testgroup.k8s.io/unversioned
|
cmd/libs/go2idl/client-gen/testoutput/clientset_generated/test_internalclientset/typed/testgroup.k8s.io/unversioned
|
||||||
cmd/libs/go2idl/conversion-gen
|
cmd/libs/go2idl/conversion-gen
|
||||||
cmd/libs/go2idl/deepcopy-gen
|
cmd/libs/go2idl/deepcopy-gen
|
||||||
|
cmd/libs/go2idl/defaulter-gen
|
||||||
cmd/libs/go2idl/generator
|
cmd/libs/go2idl/generator
|
||||||
cmd/libs/go2idl/go-to-protobuf
|
cmd/libs/go2idl/go-to-protobuf
|
||||||
cmd/libs/go2idl/go-to-protobuf/protoc-gen-gogo
|
cmd/libs/go2idl/go-to-protobuf/protoc-gen-gogo
|
||||||
|
747
vendor/k8s.io/gengo/examples/defaulter-gen/generators/defaulter.go
generated
vendored
Normal file
747
vendor/k8s.io/gengo/examples/defaulter-gen/generators/defaulter.go
generated
vendored
Normal file
@ -0,0 +1,747 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 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 generators
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"k8s.io/gengo/args"
|
||||||
|
"k8s.io/gengo/generator"
|
||||||
|
"k8s.io/gengo/namer"
|
||||||
|
"k8s.io/gengo/types"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CustomArgs is used tby the go2idl framework to pass args specific to this
|
||||||
|
// generator.
|
||||||
|
type CustomArgs struct {
|
||||||
|
ExtraPeerDirs []string // Always consider these as last-ditch possibilities for conversions.
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is the comment tag that carries parameters for defaulter generation.
|
||||||
|
const tagName = "k8s:defaulter-gen"
|
||||||
|
|
||||||
|
func extractTag(comments []string) []string {
|
||||||
|
return types.ExtractCommentTags("+", comments)[tagName]
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkTag(comments []string, require ...string) bool {
|
||||||
|
values := types.ExtractCommentTags("+", comments)[tagName]
|
||||||
|
if len(require) == 0 {
|
||||||
|
return len(values) == 1 && values[0] == ""
|
||||||
|
}
|
||||||
|
return reflect.DeepEqual(values, require)
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultFnNamer() *namer.NameStrategy {
|
||||||
|
return &namer.NameStrategy{
|
||||||
|
Prefix: "SetDefaults_",
|
||||||
|
Join: func(pre string, in []string, post string) string {
|
||||||
|
return pre + strings.Join(in, "_") + post
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func objectDefaultFnNamer() *namer.NameStrategy {
|
||||||
|
return &namer.NameStrategy{
|
||||||
|
Prefix: "SetObjectDefaults_",
|
||||||
|
Join: func(pre string, in []string, post string) string {
|
||||||
|
return pre + strings.Join(in, "_") + post
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NameSystems returns the name system used by the generators in this package.
|
||||||
|
func NameSystems() namer.NameSystems {
|
||||||
|
return namer.NameSystems{
|
||||||
|
"public": namer.NewPublicNamer(1),
|
||||||
|
"raw": namer.NewRawNamer("", nil),
|
||||||
|
"defaultfn": defaultFnNamer(),
|
||||||
|
"objectdefaultfn": objectDefaultFnNamer(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultNameSystem returns the default name system for ordering the types to be
|
||||||
|
// processed by the generators in this package.
|
||||||
|
func DefaultNameSystem() string {
|
||||||
|
return "public"
|
||||||
|
}
|
||||||
|
|
||||||
|
// defaults holds the declared defaulting functions for a given type (all defaulting functions
|
||||||
|
// are expected to be func(1))
|
||||||
|
type defaults struct {
|
||||||
|
// object is the defaulter function for a top level type (typically one with TypeMeta) that
|
||||||
|
// invokes all child defaulters. May be nil if the object defaulter has not yet been generated.
|
||||||
|
object *types.Type
|
||||||
|
// base is a defaulter function defined for a type SetDefaults_Pod which does not invoke all
|
||||||
|
// child defaults - the base defaulter alone is insufficient to default a type
|
||||||
|
base *types.Type
|
||||||
|
// additional is zero or more defaulter functions of the form SetDefaults_Pod_XXXX that can be
|
||||||
|
// included in the Object defaulter.
|
||||||
|
additional []*types.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
// All of the types in conversions map are of type "DeclarationOf" with
|
||||||
|
// the underlying type being "Func".
|
||||||
|
type defaulterFuncMap map[*types.Type]defaults
|
||||||
|
|
||||||
|
// Returns all manually-defined defaulting functions in the package.
|
||||||
|
func getManualDefaultingFunctions(context *generator.Context, pkg *types.Package, manualMap defaulterFuncMap) {
|
||||||
|
buffer := &bytes.Buffer{}
|
||||||
|
sw := generator.NewSnippetWriter(buffer, context, "$", "$")
|
||||||
|
|
||||||
|
for _, f := range pkg.Functions {
|
||||||
|
if f.Underlying == nil || f.Underlying.Kind != types.Func {
|
||||||
|
glog.Errorf("Malformed function: %#v", f)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if f.Underlying.Signature == nil {
|
||||||
|
glog.Errorf("Function without signature: %#v", f)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
signature := f.Underlying.Signature
|
||||||
|
// Check whether the function is defaulting function.
|
||||||
|
// Note that all of them have signature:
|
||||||
|
// object: func SetObjectDefaults_inType(*inType)
|
||||||
|
// base: func SetDefaults_inType(*inType)
|
||||||
|
// additional: func SetDefaults_inType_Qualifier(*inType)
|
||||||
|
if signature.Receiver != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(signature.Parameters) != 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(signature.Results) != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
inType := signature.Parameters[0]
|
||||||
|
if inType.Kind != types.Pointer {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Check if this is the primary defaulter.
|
||||||
|
args := defaultingArgsFromType(inType.Elem)
|
||||||
|
sw.Do("$.inType|defaultfn$", args)
|
||||||
|
switch {
|
||||||
|
case f.Name.Name == buffer.String():
|
||||||
|
key := inType.Elem
|
||||||
|
// We might scan the same package twice, and that's OK.
|
||||||
|
v, ok := manualMap[key]
|
||||||
|
if ok && v.base != nil && v.base.Name.Package != pkg.Path {
|
||||||
|
panic(fmt.Sprintf("duplicate static defaulter defined: %#v", key))
|
||||||
|
}
|
||||||
|
v.base = f
|
||||||
|
manualMap[key] = v
|
||||||
|
glog.V(6).Infof("found base defaulter function for %s from %s", key.Name, f.Name)
|
||||||
|
// Is one of the additional defaulters - a top level defaulter on a type that is
|
||||||
|
// also invoked.
|
||||||
|
case strings.HasPrefix(f.Name.Name, buffer.String()+"_"):
|
||||||
|
key := inType.Elem
|
||||||
|
v, ok := manualMap[key]
|
||||||
|
if ok {
|
||||||
|
exists := false
|
||||||
|
for _, existing := range v.additional {
|
||||||
|
if existing.Name == f.Name {
|
||||||
|
exists = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
v.additional = append(v.additional, f)
|
||||||
|
manualMap[key] = v
|
||||||
|
glog.V(6).Infof("found additional defaulter function for %s from %s", key.Name, f.Name)
|
||||||
|
}
|
||||||
|
buffer.Reset()
|
||||||
|
sw.Do("$.inType|objectdefaultfn$", args)
|
||||||
|
if f.Name.Name == buffer.String() {
|
||||||
|
key := inType.Elem
|
||||||
|
// We might scan the same package twice, and that's OK.
|
||||||
|
v, ok := manualMap[key]
|
||||||
|
if ok && v.base != nil && v.base.Name.Package != pkg.Path {
|
||||||
|
panic(fmt.Sprintf("duplicate static defaulter defined: %#v", key))
|
||||||
|
}
|
||||||
|
v.object = f
|
||||||
|
manualMap[key] = v
|
||||||
|
glog.V(6).Infof("found object defaulter function for %s from %s", key.Name, f.Name)
|
||||||
|
}
|
||||||
|
buffer.Reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Packages(context *generator.Context, arguments *args.GeneratorArgs) generator.Packages {
|
||||||
|
boilerplate, err := arguments.LoadGoBoilerplate()
|
||||||
|
if err != nil {
|
||||||
|
glog.Fatalf("Failed loading boilerplate: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
packages := generator.Packages{}
|
||||||
|
header := append([]byte(fmt.Sprintf("// +build !%s\n\n", arguments.GeneratedBuildTag)), boilerplate...)
|
||||||
|
header = append(header, []byte(
|
||||||
|
`
|
||||||
|
// This file was autogenerated by defaulter-gen. Do not edit it manually!
|
||||||
|
|
||||||
|
`)...)
|
||||||
|
|
||||||
|
// Accumulate pre-existing default functions.
|
||||||
|
// TODO: This is too ad-hoc. We need a better way.
|
||||||
|
existingDefaulters := defaulterFuncMap{}
|
||||||
|
|
||||||
|
buffer := &bytes.Buffer{}
|
||||||
|
sw := generator.NewSnippetWriter(buffer, context, "$", "$")
|
||||||
|
|
||||||
|
// We are generating defaults only for packages that are explicitly
|
||||||
|
// passed as InputDir.
|
||||||
|
for _, i := range context.Inputs {
|
||||||
|
glog.V(5).Infof("considering pkg %q", i)
|
||||||
|
pkg := context.Universe[i]
|
||||||
|
if pkg == nil {
|
||||||
|
// If the input had no Go files, for example.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add defaulting functions.
|
||||||
|
getManualDefaultingFunctions(context, pkg, existingDefaulters)
|
||||||
|
|
||||||
|
var peerPkgs []string
|
||||||
|
if customArgs, ok := arguments.CustomArgs.(*CustomArgs); ok {
|
||||||
|
if len(customArgs.ExtraPeerDirs) > 0 {
|
||||||
|
peerPkgs = append(peerPkgs, customArgs.ExtraPeerDirs...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Make sure our peer-packages are added and fully parsed.
|
||||||
|
for _, pp := range peerPkgs {
|
||||||
|
context.AddDir(pp)
|
||||||
|
getManualDefaultingFunctions(context, context.Universe[pp], existingDefaulters)
|
||||||
|
}
|
||||||
|
|
||||||
|
typesWith := extractTag(pkg.Comments)
|
||||||
|
shouldCreateObjectDefaulterFn := func(t *types.Type) bool {
|
||||||
|
if defaults, ok := existingDefaulters[t]; ok && defaults.object != nil {
|
||||||
|
// A default generator is defined
|
||||||
|
glog.V(5).Infof(" an object defaulter already exists as %s", defaults.base.Name)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// opt-out
|
||||||
|
if checkTag(t.SecondClosestCommentLines, "false") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// opt-in
|
||||||
|
if checkTag(t.SecondClosestCommentLines, "true") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// For every k8s:defaulter-gen tag at the package level, interpret the value as a
|
||||||
|
// field name (like TypeMeta, ListMeta, ObjectMeta) and trigger defaulter generation
|
||||||
|
// for any type with any of the matching field names. Provides a more useful package
|
||||||
|
// level defaulting than global (because we only need defaulters on a subset of objects -
|
||||||
|
// usually those with TypeMeta).
|
||||||
|
if t.Kind == types.Struct && len(typesWith) > 0 {
|
||||||
|
for _, field := range t.Members {
|
||||||
|
for _, s := range typesWith {
|
||||||
|
if field.Name == s {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
newDefaulters := defaulterFuncMap{}
|
||||||
|
for _, t := range pkg.Types {
|
||||||
|
if !shouldCreateObjectDefaulterFn(t) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if namer.IsPrivateGoName(t.Name.Name) {
|
||||||
|
// We won't be able to convert to a private type.
|
||||||
|
glog.V(5).Infof(" found a type %v, but it is a private name", t)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a synthetic type we can use during generation
|
||||||
|
newDefaulters[t] = defaults{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// only generate defaulters for objects that actually have defined defaulters
|
||||||
|
// prevents empty defaulters from being registered
|
||||||
|
for {
|
||||||
|
promoted := 0
|
||||||
|
for t, d := range newDefaulters {
|
||||||
|
if d.object != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if buildCallTreeForType(t, true, existingDefaulters, newDefaulters) != nil {
|
||||||
|
args := defaultingArgsFromType(t)
|
||||||
|
sw.Do("$.inType|objectdefaultfn$", args)
|
||||||
|
newDefaulters[t] = defaults{
|
||||||
|
object: &types.Type{
|
||||||
|
Name: types.Name{
|
||||||
|
Package: pkg.Path,
|
||||||
|
Name: buffer.String(),
|
||||||
|
},
|
||||||
|
Kind: types.Func,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
buffer.Reset()
|
||||||
|
promoted++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if promoted != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// prune any types that were not used
|
||||||
|
for t, d := range newDefaulters {
|
||||||
|
if d.object == nil {
|
||||||
|
glog.V(6).Infof("did not generate defaulter for %s because no child defaulters were registered", t.Name)
|
||||||
|
delete(newDefaulters, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(newDefaulters) == 0 {
|
||||||
|
glog.V(5).Infof("no defaulters in package %s", pkg.Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
packages = append(packages,
|
||||||
|
&generator.DefaultPackage{
|
||||||
|
PackageName: filepath.Base(pkg.Path),
|
||||||
|
PackagePath: pkg.Path,
|
||||||
|
HeaderText: header,
|
||||||
|
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
|
||||||
|
return []generator.Generator{
|
||||||
|
NewGenDefaulter(arguments.OutputFileBaseName, pkg.Path, existingDefaulters, newDefaulters, peerPkgs),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
FilterFunc: func(c *generator.Context, t *types.Type) bool {
|
||||||
|
return t.Name.Package == pkg.Path
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return packages
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildCallTreeForType creates a tree of paths to fields (based on how they would be accessed in Go - pointer, elem,
|
||||||
|
// slice, or key) and the functions that should be invoked on each field. An in-order traversal of the resulting tree
|
||||||
|
// can be used to generate a Go function that invokes each nested function on the appropriate type. The return
|
||||||
|
// value may be nil if there are no functions to call on type or the type is a primitive (Defaulters can only be
|
||||||
|
// invoked on structs today). When root is true this function will not use a newDefaulter. existingDefaulters should
|
||||||
|
// contain all defaulting functions by type defined in code - newDefaulters should contain all object defaulters
|
||||||
|
// that could be or will be generated. If newDefaulters has an entry for a type, but the 'object' field is nil,
|
||||||
|
// this function skips adding that defaulter - this allows us to avoid generating object defaulter functions for
|
||||||
|
// list types that call empty defaulters.
|
||||||
|
func buildCallTreeForType(t *types.Type, root bool, existingDefaulters, newDefaulters defaulterFuncMap) *callNode {
|
||||||
|
parent := &callNode{}
|
||||||
|
|
||||||
|
if root {
|
||||||
|
// the root node is always a pointer
|
||||||
|
parent.elem = true
|
||||||
|
}
|
||||||
|
|
||||||
|
defaults, _ := existingDefaulters[t]
|
||||||
|
newDefaults, generated := newDefaulters[t]
|
||||||
|
switch {
|
||||||
|
case !root && generated && newDefaults.object != nil:
|
||||||
|
parent.call = append(parent.call, newDefaults.object)
|
||||||
|
// if we will be generating the defaulter, it by definition is a covering
|
||||||
|
// defaulter, so we halt recursion
|
||||||
|
glog.V(6).Infof("the defaulter %s will be generated as an object defaulter", t.Name)
|
||||||
|
return parent
|
||||||
|
|
||||||
|
case defaults.object != nil:
|
||||||
|
// object defaulters are always covering
|
||||||
|
parent.call = append(parent.call, defaults.object)
|
||||||
|
return parent
|
||||||
|
|
||||||
|
case defaults.base != nil:
|
||||||
|
parent.call = append(parent.call, defaults.base)
|
||||||
|
// if the base function indicates it "covers" (it already includes defaulters)
|
||||||
|
// we can halt recursion
|
||||||
|
if checkTag(defaults.base.CommentLines, "covers") {
|
||||||
|
glog.V(6).Infof("the defaulter %s indicates it covers all sub generators", t.Name)
|
||||||
|
return parent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// base has been added already, now add any additional defaulters defined for this object
|
||||||
|
parent.call = append(parent.call, defaults.additional...)
|
||||||
|
|
||||||
|
switch t.Kind {
|
||||||
|
case types.Pointer:
|
||||||
|
if child := buildCallTreeForType(t.Elem, false, existingDefaulters, newDefaulters); child != nil {
|
||||||
|
child.elem = true
|
||||||
|
parent.children = append(parent.children, *child)
|
||||||
|
}
|
||||||
|
case types.Slice, types.Array:
|
||||||
|
if child := buildCallTreeForType(t.Elem, false, existingDefaulters, newDefaulters); child != nil {
|
||||||
|
child.index = true
|
||||||
|
parent.children = append(parent.children, *child)
|
||||||
|
}
|
||||||
|
case types.Map:
|
||||||
|
if child := buildCallTreeForType(t.Elem, false, existingDefaulters, newDefaulters); child != nil {
|
||||||
|
child.key = true
|
||||||
|
parent.children = append(parent.children, *child)
|
||||||
|
}
|
||||||
|
case types.Struct:
|
||||||
|
for _, field := range t.Members {
|
||||||
|
name := field.Name
|
||||||
|
if len(name) == 0 {
|
||||||
|
if field.Type.Kind == types.Pointer {
|
||||||
|
name = field.Type.Elem.Name.Name
|
||||||
|
} else {
|
||||||
|
name = field.Type.Name.Name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if child := buildCallTreeForType(field.Type, false, existingDefaulters, newDefaulters); child != nil {
|
||||||
|
child.field = name
|
||||||
|
parent.children = append(parent.children, *child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(parent.children) == 0 && len(parent.call) == 0 {
|
||||||
|
//glog.V(6).Infof("decided type %s needs no generation", t.Name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return parent
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
runtimePackagePath = "k8s.io/kubernetes/pkg/runtime"
|
||||||
|
conversionPackagePath = "k8s.io/kubernetes/pkg/conversion"
|
||||||
|
)
|
||||||
|
|
||||||
|
// genDefaulter produces a file with a autogenerated conversions.
|
||||||
|
type genDefaulter struct {
|
||||||
|
generator.DefaultGen
|
||||||
|
targetPackage string
|
||||||
|
peerPackages []string
|
||||||
|
newDefaulters defaulterFuncMap
|
||||||
|
existingDefaulters defaulterFuncMap
|
||||||
|
imports namer.ImportTracker
|
||||||
|
typesForInit []*types.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGenDefaulter(sanitizedName, targetPackage string, existingDefaulters, newDefaulters defaulterFuncMap, peerPkgs []string) generator.Generator {
|
||||||
|
return &genDefaulter{
|
||||||
|
DefaultGen: generator.DefaultGen{
|
||||||
|
OptionalName: sanitizedName,
|
||||||
|
},
|
||||||
|
targetPackage: targetPackage,
|
||||||
|
peerPackages: peerPkgs,
|
||||||
|
newDefaulters: newDefaulters,
|
||||||
|
existingDefaulters: existingDefaulters,
|
||||||
|
imports: generator.NewImportTracker(),
|
||||||
|
typesForInit: make([]*types.Type, 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *genDefaulter) Namers(c *generator.Context) namer.NameSystems {
|
||||||
|
// Have the raw namer for this file track what it imports.
|
||||||
|
return namer.NameSystems{
|
||||||
|
"raw": namer.NewRawNamer(g.targetPackage, g.imports),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *genDefaulter) isOtherPackage(pkg string) bool {
|
||||||
|
if pkg == g.targetPackage {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(pkg, `"`+g.targetPackage+`"`) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *genDefaulter) Filter(c *generator.Context, t *types.Type) bool {
|
||||||
|
defaults, ok := g.newDefaulters[t]
|
||||||
|
if !ok || defaults.object == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
g.typesForInit = append(g.typesForInit, t)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *genDefaulter) Imports(c *generator.Context) (imports []string) {
|
||||||
|
var importLines []string
|
||||||
|
for _, singleImport := range g.imports.ImportLines() {
|
||||||
|
if g.isOtherPackage(singleImport) {
|
||||||
|
importLines = append(importLines, singleImport)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return importLines
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *genDefaulter) Init(c *generator.Context, w io.Writer) error {
|
||||||
|
sw := generator.NewSnippetWriter(w, c, "$", "$")
|
||||||
|
|
||||||
|
scheme := c.Universe.Type(types.Name{Package: runtimePackagePath, Name: "Scheme"})
|
||||||
|
schemePtr := &types.Type{
|
||||||
|
Kind: types.Pointer,
|
||||||
|
Elem: scheme,
|
||||||
|
}
|
||||||
|
sw.Do("// RegisterDefaults adds defaulters functions to the given scheme.\n", nil)
|
||||||
|
sw.Do("// Public to allow building arbitrary schemes.\n", nil)
|
||||||
|
sw.Do("// All generated defaulters are covering - they call all nested defaulters.\n", nil)
|
||||||
|
sw.Do("func RegisterDefaults(scheme $.|raw$) error {\n", schemePtr)
|
||||||
|
for _, t := range g.typesForInit {
|
||||||
|
args := defaultingArgsFromType(t)
|
||||||
|
sw.Do("scheme.AddTypeDefaultingFunc(&$.inType|raw${}, func(obj interface{}) { $.inType|objectdefaultfn$(obj.(*$.inType|raw$)) })\n", args)
|
||||||
|
}
|
||||||
|
sw.Do("return nil\n", nil)
|
||||||
|
sw.Do("}\n\n", nil)
|
||||||
|
return sw.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *genDefaulter) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
|
||||||
|
if _, ok := g.newDefaulters[t]; !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
glog.V(5).Infof("generating for type %v", t)
|
||||||
|
|
||||||
|
callTree := buildCallTreeForType(t, true, g.existingDefaulters, g.newDefaulters)
|
||||||
|
if callTree == nil {
|
||||||
|
glog.V(5).Infof(" no defaulters defined")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
i := 0
|
||||||
|
callTree.VisitInOrder(func(ancestors []*callNode, current *callNode) {
|
||||||
|
if len(current.call) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
path := callPath(append(ancestors, current))
|
||||||
|
glog.V(5).Infof(" %d: %s", i, path)
|
||||||
|
i++
|
||||||
|
})
|
||||||
|
|
||||||
|
sw := generator.NewSnippetWriter(w, c, "$", "$")
|
||||||
|
g.generateDefaulter(t, callTree, sw)
|
||||||
|
return sw.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultingArgsFromType(inType *types.Type) generator.Args {
|
||||||
|
return generator.Args{
|
||||||
|
"inType": inType,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *genDefaulter) generateDefaulter(inType *types.Type, callTree *callNode, sw *generator.SnippetWriter) {
|
||||||
|
sw.Do("func $.inType|objectdefaultfn$(in *$.inType|raw$) {\n", defaultingArgsFromType(inType))
|
||||||
|
callTree.WriteMethod("in", 0, nil, sw)
|
||||||
|
sw.Do("}\n\n", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// callNode represents an entry in a tree of Go type accessors - the path from the root to a leaf represents
|
||||||
|
// how in Go code an access would be performed. For example, if a defaulting function exists on a container
|
||||||
|
// lifecycle hook, to invoke that defaulter correctly would require this Go code:
|
||||||
|
//
|
||||||
|
// for i := range pod.Spec.Containers {
|
||||||
|
// o := &pod.Spec.Containers[i]
|
||||||
|
// if o.LifecycleHook != nil {
|
||||||
|
// SetDefaults_LifecycleHook(o.LifecycleHook)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// That would be represented by a call tree like:
|
||||||
|
//
|
||||||
|
// callNode
|
||||||
|
// field: "Spec"
|
||||||
|
// children:
|
||||||
|
// - field: "Containers"
|
||||||
|
// children:
|
||||||
|
// - index: true
|
||||||
|
// children:
|
||||||
|
// - field: "LifecycleHook"
|
||||||
|
// elem: true
|
||||||
|
// call:
|
||||||
|
// - SetDefaults_LifecycleHook
|
||||||
|
//
|
||||||
|
// which we can traverse to build that Go struct (you must call the field Spec, then Containers, then range over
|
||||||
|
// that field, then check whether the LifecycleHook field is nil, before calling SetDefaults_LifecycleHook on
|
||||||
|
// the pointer to that field).
|
||||||
|
type callNode struct {
|
||||||
|
// field is the name of the Go member to access
|
||||||
|
field string
|
||||||
|
// key is true if this is a map and we must range over the key and values
|
||||||
|
key bool
|
||||||
|
// index is true if this is a slice and we must range over the slice values
|
||||||
|
index bool
|
||||||
|
// elem is true if the previous elements refer to a pointer (typically just field)
|
||||||
|
elem bool
|
||||||
|
|
||||||
|
// call is all of the functions that must be invoked on this particular node, in order
|
||||||
|
call []*types.Type
|
||||||
|
// children is the child call nodes that must also be traversed
|
||||||
|
children []callNode
|
||||||
|
}
|
||||||
|
|
||||||
|
// CallNodeVisitorFunc is a function for visiting a call tree. ancestors is the list of all parents
|
||||||
|
// of this node to the root of the tree - will be empty at the root.
|
||||||
|
type CallNodeVisitorFunc func(ancestors []*callNode, node *callNode)
|
||||||
|
|
||||||
|
func (n *callNode) VisitInOrder(fn CallNodeVisitorFunc) {
|
||||||
|
n.visitInOrder(nil, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *callNode) visitInOrder(ancestors []*callNode, fn CallNodeVisitorFunc) {
|
||||||
|
fn(ancestors, n)
|
||||||
|
ancestors = append(ancestors, n)
|
||||||
|
for i := range n.children {
|
||||||
|
n.children[i].visitInOrder(ancestors, fn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
indexVariables = "ijklmnop"
|
||||||
|
localVariables = "abcdefgh"
|
||||||
|
)
|
||||||
|
|
||||||
|
// varsForDepth creates temporary variables guaranteed to be unique within lexical Go scopes
|
||||||
|
// of this depth in a function. It uses canonical Go loop variables for the first 7 levels
|
||||||
|
// and then resorts to uglier prefixes.
|
||||||
|
func varsForDepth(depth int) (index, local string) {
|
||||||
|
if depth > len(indexVariables) {
|
||||||
|
index = fmt.Sprintf("i%d", depth)
|
||||||
|
} else {
|
||||||
|
index = indexVariables[depth : depth+1]
|
||||||
|
}
|
||||||
|
if depth > len(localVariables) {
|
||||||
|
local = fmt.Sprintf("local%d", depth)
|
||||||
|
} else {
|
||||||
|
local = localVariables[depth : depth+1]
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeCalls generates a list of function calls based on the calls field for the provided variable
|
||||||
|
// name and pointer.
|
||||||
|
func (n *callNode) writeCalls(varName string, isVarPointer bool, sw *generator.SnippetWriter) {
|
||||||
|
accessor := varName
|
||||||
|
if !isVarPointer {
|
||||||
|
accessor = "&" + accessor
|
||||||
|
}
|
||||||
|
for _, fn := range n.call {
|
||||||
|
sw.Do("$.fn|raw$($.var$)\n", generator.Args{
|
||||||
|
"fn": fn,
|
||||||
|
"var": accessor,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteMethod performs an in-order traversal of the calltree, generating loops and if blocks as necessary
|
||||||
|
// to correctly turn the call tree into a method body that invokes all calls on all child nodes of the call tree.
|
||||||
|
// Depth is used to generate local variables at the proper depth.
|
||||||
|
func (n *callNode) WriteMethod(varName string, depth int, ancestors []*callNode, sw *generator.SnippetWriter) {
|
||||||
|
// if len(n.call) > 0 {
|
||||||
|
// sw.Do(fmt.Sprintf("// %s\n", callPath(append(ancestors, n)).String()), nil)
|
||||||
|
// }
|
||||||
|
|
||||||
|
if len(n.field) > 0 {
|
||||||
|
varName = varName + "." + n.field
|
||||||
|
}
|
||||||
|
|
||||||
|
index, local := varsForDepth(depth)
|
||||||
|
vars := generator.Args{
|
||||||
|
"index": index,
|
||||||
|
"local": local,
|
||||||
|
"var": varName,
|
||||||
|
}
|
||||||
|
|
||||||
|
isPointer := n.elem
|
||||||
|
if isPointer && len(ancestors) > 0 {
|
||||||
|
sw.Do("if $.var$ != nil {\n", vars)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case n.index:
|
||||||
|
sw.Do("for $.index$ := range $.var$ {\n", vars)
|
||||||
|
sw.Do("$.local$ := &$.var$[$.index$]\n", vars)
|
||||||
|
n.writeCalls(local, true, sw)
|
||||||
|
for i := range n.children {
|
||||||
|
n.children[i].WriteMethod(local, depth+1, append(ancestors, n), sw)
|
||||||
|
}
|
||||||
|
sw.Do("}\n", nil)
|
||||||
|
case n.key:
|
||||||
|
default:
|
||||||
|
n.writeCalls(varName, isPointer, sw)
|
||||||
|
for i := range n.children {
|
||||||
|
n.children[i].WriteMethod(varName, depth, append(ancestors, n), sw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if isPointer && len(ancestors) > 0 {
|
||||||
|
sw.Do("}\n", nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type callPath []*callNode
|
||||||
|
|
||||||
|
// String prints a representation of a callPath that roughly approximates what a Go accessor
|
||||||
|
// would look like. Used for debugging only.
|
||||||
|
func (path callPath) String() string {
|
||||||
|
if len(path) == 0 {
|
||||||
|
return "<none>"
|
||||||
|
}
|
||||||
|
var parts []string
|
||||||
|
for _, p := range path {
|
||||||
|
last := len(parts) - 1
|
||||||
|
switch {
|
||||||
|
case p.elem:
|
||||||
|
if len(parts) > 0 {
|
||||||
|
parts[last] = "*" + parts[last]
|
||||||
|
} else {
|
||||||
|
parts = append(parts, "*")
|
||||||
|
}
|
||||||
|
case p.index:
|
||||||
|
if len(parts) > 0 {
|
||||||
|
parts[last] = parts[last] + "[i]"
|
||||||
|
} else {
|
||||||
|
parts = append(parts, "[i]")
|
||||||
|
}
|
||||||
|
case p.key:
|
||||||
|
if len(parts) > 0 {
|
||||||
|
parts[last] = parts[last] + "[key]"
|
||||||
|
} else {
|
||||||
|
parts = append(parts, "[key]")
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if len(p.field) > 0 {
|
||||||
|
parts = append(parts, p.field)
|
||||||
|
} else {
|
||||||
|
parts = append(parts, "<root>")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var calls []string
|
||||||
|
for _, fn := range path[len(path)-1].call {
|
||||||
|
calls = append(calls, fn.Name.String())
|
||||||
|
}
|
||||||
|
if len(calls) == 0 {
|
||||||
|
calls = append(calls, "<none>")
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(parts, ".") + " calls " + strings.Join(calls, ", ")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user