...

Source file src/sigs.k8s.io/structured-merge-diff/v4/fieldpath/path.go

Documentation: sigs.k8s.io/structured-merge-diff/v4/fieldpath

     1  /*
     2  Copyright 2018 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 fieldpath
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"sigs.k8s.io/structured-merge-diff/v4/value"
    24  )
    25  
    26  // Path describes how to select a potentially deeply-nested child field given a
    27  // containing object.
    28  type Path []PathElement
    29  
    30  func (fp Path) String() string {
    31  	strs := make([]string, len(fp))
    32  	for i := range fp {
    33  		strs[i] = fp[i].String()
    34  	}
    35  	return strings.Join(strs, "")
    36  }
    37  
    38  // Equals returns true if the two paths are equivalent.
    39  func (fp Path) Equals(fp2 Path) bool {
    40  	if len(fp) != len(fp2) {
    41  		return false
    42  	}
    43  	for i := range fp {
    44  		if !fp[i].Equals(fp2[i]) {
    45  			return false
    46  		}
    47  	}
    48  	return true
    49  }
    50  
    51  // Less provides a lexical order for Paths.
    52  func (fp Path) Compare(rhs Path) int {
    53  	i := 0
    54  	for {
    55  		if i >= len(fp) && i >= len(rhs) {
    56  			// Paths are the same length and all items are equal.
    57  			return 0
    58  		}
    59  		if i >= len(fp) {
    60  			// LHS is shorter.
    61  			return -1
    62  		}
    63  		if i >= len(rhs) {
    64  			// RHS is shorter.
    65  			return 1
    66  		}
    67  		if c := fp[i].Compare(rhs[i]); c != 0 {
    68  			return c
    69  		}
    70  		// The items are equal; continue.
    71  		i++
    72  	}
    73  }
    74  
    75  func (fp Path) Copy() Path {
    76  	new := make(Path, len(fp))
    77  	copy(new, fp)
    78  	return new
    79  }
    80  
    81  // MakePath constructs a Path. The parts may be PathElements, ints, strings.
    82  func MakePath(parts ...interface{}) (Path, error) {
    83  	var fp Path
    84  	for _, p := range parts {
    85  		switch t := p.(type) {
    86  		case PathElement:
    87  			fp = append(fp, t)
    88  		case int:
    89  			// TODO: Understand schema and object and convert this to the
    90  			// FieldSpecifier below if appropriate.
    91  			fp = append(fp, PathElement{Index: &t})
    92  		case string:
    93  			fp = append(fp, PathElement{FieldName: &t})
    94  		case *value.FieldList:
    95  			if len(*t) == 0 {
    96  				return nil, fmt.Errorf("associative list key type path elements must have at least one key (got zero)")
    97  			}
    98  			fp = append(fp, PathElement{Key: t})
    99  		case value.Value:
   100  			// TODO: understand schema and verify that this is a set type
   101  			// TODO: make a copy of t
   102  			fp = append(fp, PathElement{Value: &t})
   103  		default:
   104  			return nil, fmt.Errorf("unable to make %#v into a path element", p)
   105  		}
   106  	}
   107  	return fp, nil
   108  }
   109  
   110  // MakePathOrDie panics if parts can't be turned into a path. Good for things
   111  // that are known at complie time.
   112  func MakePathOrDie(parts ...interface{}) Path {
   113  	fp, err := MakePath(parts...)
   114  	if err != nil {
   115  		panic(err)
   116  	}
   117  	return fp
   118  }
   119  

View as plain text