...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/options.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apiserver/schema

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package schema
    18  
    19  import (
    20  	"strconv"
    21  	"strings"
    22  )
    23  
    24  // UnknownFieldPathOptions allow for tracking paths to unknown fields.
    25  type UnknownFieldPathOptions struct {
    26  	// TrackUnknownFieldPaths determines whether or not unknown field
    27  	// paths should be stored or not.
    28  	TrackUnknownFieldPaths bool
    29  	// ParentPath builds the path to unknown fields as the object
    30  	// is recursively traversed.
    31  	ParentPath []string
    32  	// UnknownFieldPaths is the list of all unknown fields identified.
    33  	UnknownFieldPaths []string
    34  }
    35  
    36  // RecordUnknownFields adds a path to an unknown field to the
    37  // record of UnknownFieldPaths, if TrackUnknownFieldPaths is true
    38  func (o *UnknownFieldPathOptions) RecordUnknownField(field string) {
    39  	if !o.TrackUnknownFieldPaths {
    40  		return
    41  	}
    42  	l := len(o.ParentPath)
    43  	o.AppendKey(field)
    44  	o.UnknownFieldPaths = append(o.UnknownFieldPaths, strings.Join(o.ParentPath, ""))
    45  	o.ParentPath = o.ParentPath[:l]
    46  }
    47  
    48  // AppendKey adds a key (i.e. field) to the current parent
    49  // path, if TrackUnknownFieldPaths is true.
    50  func (o *UnknownFieldPathOptions) AppendKey(key string) {
    51  	if !o.TrackUnknownFieldPaths {
    52  		return
    53  	}
    54  	if len(o.ParentPath) > 0 {
    55  		o.ParentPath = append(o.ParentPath, ".")
    56  	}
    57  	o.ParentPath = append(o.ParentPath, key)
    58  }
    59  
    60  // AppendIndex adds an index to the most recent field of
    61  // the current parent path, if TrackUnknownFieldPaths is true.
    62  func (o *UnknownFieldPathOptions) AppendIndex(index int) {
    63  	if !o.TrackUnknownFieldPaths {
    64  		return
    65  	}
    66  	o.ParentPath = append(o.ParentPath, "[", strconv.Itoa(index), "]")
    67  }
    68  

View as plain text