Integrate defaults marker and remove ContainerPort defaulter
This commit is contained in:
271
vendor/k8s.io/gengo/examples/defaulter-gen/generators/defaulter.go
generated
vendored
271
vendor/k8s.io/gengo/examples/defaulter-gen/generators/defaulter.go
generated
vendored
@@ -18,6 +18,7 @@ package generators
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
@@ -38,16 +39,45 @@ type CustomArgs struct {
|
||||
ExtraPeerDirs []string // Always consider these as last-ditch possibilities for conversions.
|
||||
}
|
||||
|
||||
var typeZeroValue = map[string]interface{}{
|
||||
"uint": 0.,
|
||||
"uint8": 0.,
|
||||
"uint16": 0.,
|
||||
"uint32": 0.,
|
||||
"uint64": 0.,
|
||||
"int": 0.,
|
||||
"int8": 0.,
|
||||
"int16": 0.,
|
||||
"int32": 0.,
|
||||
"int64": 0.,
|
||||
"byte": 0,
|
||||
"float64": 0.,
|
||||
"float32": 0.,
|
||||
"bool": false,
|
||||
"time.Time": "",
|
||||
"string": "",
|
||||
"integer": 0.,
|
||||
"number": 0.,
|
||||
"boolean": false,
|
||||
"[]byte": "", // base64 encoded characters
|
||||
"interface{}": interface{}(nil),
|
||||
}
|
||||
|
||||
// These are the comment tags that carry parameters for defaulter generation.
|
||||
const tagName = "k8s:defaulter-gen"
|
||||
const intputTagName = "k8s:defaulter-gen-input"
|
||||
const inputTagName = "k8s:defaulter-gen-input"
|
||||
const defaultTagName = "default"
|
||||
|
||||
func extractDefaultTag(comments []string) []string {
|
||||
return types.ExtractCommentTags("+", comments)[defaultTagName]
|
||||
}
|
||||
|
||||
func extractTag(comments []string) []string {
|
||||
return types.ExtractCommentTags("+", comments)[tagName]
|
||||
}
|
||||
|
||||
func extractInputTag(comments []string) []string {
|
||||
return types.ExtractCommentTags("+", comments)[intputTagName]
|
||||
return types.ExtractCommentTags("+", comments)[inputTagName]
|
||||
}
|
||||
|
||||
func checkTag(comments []string, require ...string) bool {
|
||||
@@ -401,6 +431,112 @@ func newCallTreeForType(existingDefaulters, newDefaulters defaulterFuncMap) *cal
|
||||
}
|
||||
}
|
||||
|
||||
func resolveTypeAndDepth(t *types.Type) (*types.Type, int) {
|
||||
var prev *types.Type
|
||||
depth := 0
|
||||
for prev != t {
|
||||
prev = t
|
||||
if t.Kind == types.Alias {
|
||||
t = t.Underlying
|
||||
} else if t.Kind == types.Pointer {
|
||||
t = t.Elem
|
||||
depth += 1
|
||||
}
|
||||
}
|
||||
return t, depth
|
||||
}
|
||||
|
||||
// getNestedDefault returns the first default value when resolving alias types
|
||||
func getNestedDefault(t *types.Type) string {
|
||||
var prev *types.Type
|
||||
for prev != t {
|
||||
prev = t
|
||||
defaultMap := extractDefaultTag(t.CommentLines)
|
||||
if len(defaultMap) == 1 && defaultMap[0] != "" {
|
||||
return defaultMap[0]
|
||||
}
|
||||
if t.Kind == types.Alias {
|
||||
t = t.Underlying
|
||||
} else if t.Kind == types.Pointer {
|
||||
t = t.Elem
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func mustEnforceDefault(t *types.Type, depth int, omitEmpty bool) (interface{}, error) {
|
||||
if depth > 0 {
|
||||
return nil, nil
|
||||
}
|
||||
switch t.Kind {
|
||||
case types.Pointer, types.Map, types.Slice, types.Array, types.Interface:
|
||||
return nil, nil
|
||||
case types.Struct:
|
||||
return map[string]interface{}{}, nil
|
||||
case types.Builtin:
|
||||
if !omitEmpty {
|
||||
if zero, ok := typeZeroValue[t.String()]; ok {
|
||||
return zero, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("please add type %v to typeZeroValue struct", t)
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("not sure how to enforce default for %v", t.Kind)
|
||||
}
|
||||
}
|
||||
|
||||
func populateDefaultValue(node *callNode, t *types.Type, tags string, commentLines []string) *callNode {
|
||||
defaultMap := extractDefaultTag(commentLines)
|
||||
var defaultString string
|
||||
if len(defaultMap) == 1 {
|
||||
defaultString = defaultMap[0]
|
||||
}
|
||||
|
||||
t, depth := resolveTypeAndDepth(t)
|
||||
if depth > 0 && defaultString == "" {
|
||||
defaultString = getNestedDefault(t)
|
||||
}
|
||||
if len(defaultMap) > 1 {
|
||||
klog.Fatalf("Found more than one default tag for %v", t.Kind)
|
||||
} else if len(defaultMap) == 0 {
|
||||
return node
|
||||
}
|
||||
var defaultValue interface{}
|
||||
if err := json.Unmarshal([]byte(defaultString), &defaultValue); err != nil {
|
||||
klog.Fatalf("Failed to unmarshal default: %v", err)
|
||||
}
|
||||
|
||||
omitEmpty := strings.Contains(reflect.StructTag(tags).Get("json"), "omitempty")
|
||||
if enforced, err := mustEnforceDefault(t, depth, omitEmpty); err != nil {
|
||||
klog.Fatal(err)
|
||||
} else if enforced != nil {
|
||||
if defaultValue != nil {
|
||||
if reflect.DeepEqual(defaultValue, enforced) {
|
||||
// If the default value annotation matches the default value for the type,
|
||||
// do not generate any defaulting function
|
||||
return node
|
||||
} else {
|
||||
enforcedJSON, _ := json.Marshal(enforced)
|
||||
klog.Fatalf("Invalid default value (%#v) for non-pointer/non-omitempty. If specified, must be: %v", defaultValue, string(enforcedJSON))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// callNodes are not automatically generated for primitive types. Generate one if the callNode does not exist
|
||||
if node == nil {
|
||||
node = &callNode{}
|
||||
node.markerOnly = true
|
||||
}
|
||||
|
||||
node.defaultIsPrimitive = t.IsPrimitive()
|
||||
node.defaultType = t.String()
|
||||
node.defaultValue = defaultString
|
||||
node.defaultDepth = depth
|
||||
return node
|
||||
}
|
||||
|
||||
// build 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
|
||||
@@ -473,12 +609,19 @@ func (c *callTreeForType) build(t *types.Type, root bool) *callNode {
|
||||
child.elem = true
|
||||
}
|
||||
parent.children = append(parent.children, *child)
|
||||
} else if member := populateDefaultValue(nil, t.Elem, "", t.Elem.CommentLines); member != nil {
|
||||
member.index = true
|
||||
parent.children = append(parent.children, *member)
|
||||
}
|
||||
case types.Map:
|
||||
if child := c.build(t.Elem, false); child != nil {
|
||||
child.key = true
|
||||
parent.children = append(parent.children, *child)
|
||||
} else if member := populateDefaultValue(nil, t.Elem, "", t.Elem.CommentLines); member != nil {
|
||||
member.key = true
|
||||
parent.children = append(parent.children, *member)
|
||||
}
|
||||
|
||||
case types.Struct:
|
||||
for _, field := range t.Members {
|
||||
name := field.Name
|
||||
@@ -491,7 +634,11 @@ func (c *callTreeForType) build(t *types.Type, root bool) *callNode {
|
||||
}
|
||||
if child := c.build(field.Type, false); child != nil {
|
||||
child.field = name
|
||||
populateDefaultValue(child, field.Type, field.Tags, field.CommentLines)
|
||||
parent.children = append(parent.children, *child)
|
||||
} else if member := populateDefaultValue(nil, field.Type, field.Tags, field.CommentLines); member != nil {
|
||||
member.field = name
|
||||
parent.children = append(parent.children, *member)
|
||||
}
|
||||
}
|
||||
case types.Alias:
|
||||
@@ -676,6 +823,27 @@ type callNode struct {
|
||||
call []*types.Type
|
||||
// children is the child call nodes that must also be traversed
|
||||
children []callNode
|
||||
|
||||
// defaultValue is the defaultValue of a callNode struct
|
||||
// Only primitive types and pointer types are eligible to have a default value
|
||||
defaultValue string
|
||||
|
||||
// defaultIsPrimitive is used to determine how to assign the default value.
|
||||
// Primitive types will be directly assigned while complex types will use JSON unmarshalling
|
||||
defaultIsPrimitive bool
|
||||
|
||||
// markerOnly is true if the callNode exists solely to fill in a default value
|
||||
markerOnly bool
|
||||
|
||||
// defaultDepth is used to determine pointer level of the default value
|
||||
// For example 1 corresponds to setting a default value and taking its pointer while
|
||||
// 2 corresponds to setting a default value and taking its pointer's pointer
|
||||
// 0 implies that no pointers are used
|
||||
defaultDepth int
|
||||
|
||||
// defaultType is the type of the default value.
|
||||
// Only populated if defaultIsPrimitive is true
|
||||
defaultType string
|
||||
}
|
||||
|
||||
// CallNodeVisitorFunc is a function for visiting a call tree. ancestors is the list of all parents
|
||||
@@ -731,6 +899,85 @@ func (n *callNode) writeCalls(varName string, isVarPointer bool, sw *generator.S
|
||||
}
|
||||
}
|
||||
|
||||
func (n *callNode) writeDefaulter(varName string, index string, isVarPointer bool, sw *generator.SnippetWriter) {
|
||||
if n.defaultValue == "" {
|
||||
return
|
||||
}
|
||||
varPointer := varName
|
||||
if !isVarPointer {
|
||||
varPointer = "&" + varPointer
|
||||
}
|
||||
|
||||
args := generator.Args{
|
||||
"defaultValue": n.defaultValue,
|
||||
"varPointer": varPointer,
|
||||
"varName": varName,
|
||||
"index": index,
|
||||
"varDepth": n.defaultDepth,
|
||||
"varType": n.defaultType,
|
||||
}
|
||||
|
||||
if n.index {
|
||||
sw.Do("if reflect.ValueOf($.var$[$.index$]).IsZero() {\n", generator.Args{"var": varName, "index": index})
|
||||
if n.defaultIsPrimitive {
|
||||
if n.defaultDepth > 0 {
|
||||
sw.Do("var ptrVar$.varDepth$ $.varType$ = $.defaultValue$\n", args)
|
||||
for i := n.defaultDepth; i > 0; i-- {
|
||||
sw.Do("ptrVar$.ptri$ := &ptrVar$.i$\n", generator.Args{"i": fmt.Sprintf("%d", i), "ptri": fmt.Sprintf("%d", (i - 1))})
|
||||
}
|
||||
sw.Do("$.varName$[$.index$] = ptrVar0", args)
|
||||
} else {
|
||||
sw.Do("$.varName$[$.index$] = $.defaultValue$", args)
|
||||
}
|
||||
} else {
|
||||
sw.Do("if err := json.Unmarshal([]byte(`$.defaultValue$`), $.varPointer$[$.index$]); err != nil {\n", args)
|
||||
sw.Do("panic(err)\n", nil)
|
||||
sw.Do("}\n", nil)
|
||||
}
|
||||
} else if n.key {
|
||||
mapDefaultVar := index + "_default"
|
||||
args["mapDefaultVar"] = mapDefaultVar
|
||||
sw.Do("if reflect.ValueOf($.var$[$.index$]).IsZero() {\n", generator.Args{"var": varName, "index": index})
|
||||
|
||||
if n.defaultIsPrimitive {
|
||||
if n.defaultDepth > 0 {
|
||||
sw.Do("var ptrVar$.varDepth$ $.varType$ = $.defaultValue$\n", args)
|
||||
for i := n.defaultDepth; i > 0; i-- {
|
||||
sw.Do("ptrVar$.ptri$ := &ptrVar$.i$\n", generator.Args{"i": fmt.Sprintf("%d", i), "ptri": fmt.Sprintf("%d", (i - 1))})
|
||||
}
|
||||
sw.Do("$.varName$[$.index$] = ptrVar0", args)
|
||||
} else {
|
||||
sw.Do("$.varName$[$.index$] = $.defaultValue$", args)
|
||||
}
|
||||
} else {
|
||||
sw.Do("$.mapDefaultVar$ := $.varName$[$.index$]\n", args)
|
||||
sw.Do("if err := json.Unmarshal([]byte(`$.defaultValue$`), &$.mapDefaultVar$); err != nil {\n", args)
|
||||
sw.Do("panic(err)\n", nil)
|
||||
sw.Do("}\n", nil)
|
||||
sw.Do("$.varName$[$.index$] = $.mapDefaultVar$\n", args)
|
||||
}
|
||||
} else {
|
||||
sw.Do("if reflect.ValueOf($.var$).IsZero() {\n", generator.Args{"var": varName})
|
||||
|
||||
if n.defaultIsPrimitive {
|
||||
if n.defaultDepth > 0 {
|
||||
sw.Do("var ptrVar$.varDepth$ $.varType$ = $.defaultValue$\n", args)
|
||||
for i := n.defaultDepth; i > 0; i-- {
|
||||
sw.Do("ptrVar$.ptri$ := &ptrVar$.i$\n", generator.Args{"i": fmt.Sprintf("%d", i), "ptri": fmt.Sprintf("%d", (i - 1))})
|
||||
}
|
||||
sw.Do("$.varName$ = ptrVar0", args)
|
||||
} else {
|
||||
sw.Do("$.varName$ = $.defaultValue$", args)
|
||||
}
|
||||
} else {
|
||||
sw.Do("if err := json.Unmarshal([]byte(`$.defaultValue$`), $.varPointer$); err != nil {\n", args)
|
||||
sw.Do("panic(err)\n", nil)
|
||||
sw.Do("}\n", nil)
|
||||
}
|
||||
}
|
||||
sw.Do("}\n", nil)
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -758,19 +1005,31 @@ func (n *callNode) WriteMethod(varName string, depth int, ancestors []*callNode,
|
||||
switch {
|
||||
case n.index:
|
||||
sw.Do("for $.index$ := range $.var$ {\n", vars)
|
||||
if n.elem {
|
||||
sw.Do("$.local$ := $.var$[$.index$]\n", vars)
|
||||
} else {
|
||||
sw.Do("$.local$ := &$.var$[$.index$]\n", vars)
|
||||
if !n.markerOnly {
|
||||
if n.elem {
|
||||
sw.Do("$.local$ := $.var$[$.index$]\n", vars)
|
||||
} else {
|
||||
sw.Do("$.local$ := &$.var$[$.index$]\n", vars)
|
||||
}
|
||||
}
|
||||
|
||||
n.writeDefaulter(varName, index, isPointer, sw)
|
||||
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:
|
||||
if n.defaultValue != "" {
|
||||
// Map keys are typed and cannot share the same index variable as arrays and other maps
|
||||
index = index + "_" + ancestors[len(ancestors)-1].field
|
||||
vars["index"] = index
|
||||
sw.Do("for $.index$ := range $.var$ {\n", vars)
|
||||
n.writeDefaulter(varName, index, isPointer, sw)
|
||||
sw.Do("}\n", nil)
|
||||
}
|
||||
default:
|
||||
n.writeDefaulter(varName, index, isPointer, sw)
|
||||
n.writeCalls(varName, isPointer, sw)
|
||||
for i := range n.children {
|
||||
n.children[i].WriteMethod(varName, depth, append(ancestors, n), sw)
|
||||
|
2
vendor/k8s.io/gengo/examples/set-gen/generators/sets.go
generated
vendored
2
vendor/k8s.io/gengo/examples/set-gen/generators/sets.go
generated
vendored
@@ -187,7 +187,7 @@ type $.type|public$ map[$.type|raw$]Empty
|
||||
|
||||
// New$.type|public$ creates a $.type|public$ from a list of values.
|
||||
func New$.type|public$(items ...$.type|raw$) $.type|public$ {
|
||||
ss := $.type|public${}
|
||||
ss := make($.type|public$, len(items))
|
||||
ss.Insert(items...)
|
||||
return ss
|
||||
}
|
||||
|
2
vendor/k8s.io/gengo/examples/set-gen/sets/byte.go
generated
vendored
2
vendor/k8s.io/gengo/examples/set-gen/sets/byte.go
generated
vendored
@@ -28,7 +28,7 @@ type Byte map[byte]Empty
|
||||
|
||||
// NewByte creates a Byte from a list of values.
|
||||
func NewByte(items ...byte) Byte {
|
||||
ss := Byte{}
|
||||
ss := make(Byte, len(items))
|
||||
ss.Insert(items...)
|
||||
return ss
|
||||
}
|
||||
|
2
vendor/k8s.io/gengo/examples/set-gen/sets/int.go
generated
vendored
2
vendor/k8s.io/gengo/examples/set-gen/sets/int.go
generated
vendored
@@ -28,7 +28,7 @@ type Int map[int]Empty
|
||||
|
||||
// NewInt creates a Int from a list of values.
|
||||
func NewInt(items ...int) Int {
|
||||
ss := Int{}
|
||||
ss := make(Int, len(items))
|
||||
ss.Insert(items...)
|
||||
return ss
|
||||
}
|
||||
|
2
vendor/k8s.io/gengo/examples/set-gen/sets/int64.go
generated
vendored
2
vendor/k8s.io/gengo/examples/set-gen/sets/int64.go
generated
vendored
@@ -28,7 +28,7 @@ type Int64 map[int64]Empty
|
||||
|
||||
// NewInt64 creates a Int64 from a list of values.
|
||||
func NewInt64(items ...int64) Int64 {
|
||||
ss := Int64{}
|
||||
ss := make(Int64, len(items))
|
||||
ss.Insert(items...)
|
||||
return ss
|
||||
}
|
||||
|
2
vendor/k8s.io/gengo/examples/set-gen/sets/string.go
generated
vendored
2
vendor/k8s.io/gengo/examples/set-gen/sets/string.go
generated
vendored
@@ -28,7 +28,7 @@ type String map[string]Empty
|
||||
|
||||
// NewString creates a String from a list of values.
|
||||
func NewString(items ...string) String {
|
||||
ss := String{}
|
||||
ss := make(String, len(items))
|
||||
ss.Insert(items...)
|
||||
return ss
|
||||
}
|
||||
|
2
vendor/k8s.io/gengo/generator/snippet_writer.go
generated
vendored
2
vendor/k8s.io/gengo/generator/snippet_writer.go
generated
vendored
@@ -57,7 +57,7 @@ func NewSnippetWriter(w io.Writer, c *Context, left, right string) *SnippetWrite
|
||||
|
||||
// Do parses format and runs args through it. You can have arbitrary logic in
|
||||
// the format (see the text/template documentation), but consider running many
|
||||
// short templaces, with ordinary go logic in between--this may be more
|
||||
// short templates with ordinary go logic in between--this may be more
|
||||
// readable. Do is chainable. Any error causes every other call to do to be
|
||||
// ignored, and the error will be returned by Error(). So you can check it just
|
||||
// once, at the end of your function.
|
||||
|
20
vendor/k8s.io/gengo/parser/parse.go
generated
vendored
20
vendor/k8s.io/gengo/parser/parse.go
generated
vendored
@@ -20,6 +20,7 @@ import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/build"
|
||||
"go/constant"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
tc "go/types"
|
||||
@@ -335,7 +336,8 @@ func (b *Builder) addDir(dir string, userRequested bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var regexErrPackageNotFound = regexp.MustCompile(`^unable to import ".*?": cannot find package ".*?" in any of:`)
|
||||
// regexErrPackageNotFound helps test the expected error for not finding a package.
|
||||
var regexErrPackageNotFound = regexp.MustCompile(`^unable to import ".*?":.*`)
|
||||
|
||||
func isErrPackageNotFound(err error) bool {
|
||||
return regexErrPackageNotFound.MatchString(err.Error())
|
||||
@@ -847,6 +849,22 @@ func (b *Builder) addConstant(u types.Universe, useName *types.Name, in *tc.Cons
|
||||
out := u.Constant(name)
|
||||
out.Kind = types.DeclarationOf
|
||||
out.Underlying = b.walkType(u, nil, in.Type())
|
||||
|
||||
var constval string
|
||||
|
||||
// For strings, we use `StringVal()` to get the un-truncated,
|
||||
// un-quoted string. For other values, `.String()` is preferable to
|
||||
// get something relatively human readable (especially since for
|
||||
// floating point types, `ExactString()` will generate numeric
|
||||
// expressions using `big.(*Float).Text()`.
|
||||
switch in.Val().Kind() {
|
||||
case constant.String:
|
||||
constval = constant.StringVal(in.Val())
|
||||
default:
|
||||
constval = in.Val().String()
|
||||
}
|
||||
|
||||
out.ConstValue = &constval
|
||||
return out
|
||||
}
|
||||
|
||||
|
7
vendor/k8s.io/gengo/types/types.go
generated
vendored
7
vendor/k8s.io/gengo/types/types.go
generated
vendored
@@ -341,6 +341,13 @@ type Type struct {
|
||||
// If Kind == func, this is the signature of the function.
|
||||
Signature *Signature
|
||||
|
||||
// ConstValue contains a stringified constant value if
|
||||
// Kind == DeclarationOf and this is a constant value
|
||||
// declaration. For string constants, this field contains
|
||||
// the entire, un-quoted value. For other types, it contains
|
||||
// a human-readable literal.
|
||||
ConstValue *string
|
||||
|
||||
// TODO: Add:
|
||||
// * channel direction
|
||||
// * array length
|
||||
|
Reference in New Issue
Block a user