...

Source file src/cuelang.org/go/internal/core/path/selector.go

Documentation: cuelang.org/go/internal/core/path

     1  // Copyright 2022 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package path provides utilities for converting cue.Selectors and cue.Paths to
    16  // internal equivalents.
    17  package path
    18  
    19  import (
    20  	"math/bits"
    21  
    22  	"cuelang.org/go/cue"
    23  	"cuelang.org/go/internal/core/adt"
    24  	"cuelang.org/go/internal/core/runtime"
    25  )
    26  
    27  // ToFeatureType converts a SelectorType constant to a FeatureType. It assumes a single label bit is set.
    28  func ToFeatureType(t cue.SelectorType) adt.FeatureType {
    29  	t = t.LabelType()
    30  	return adt.FeatureType(bits.Len16(uint16(t)))
    31  }
    32  
    33  // MakeFeature converts a cue.Selector to an adt.Feature for a given runtime.
    34  func MakeFeature(r *runtime.Runtime, s cue.Selector) adt.Feature {
    35  	constraintType := s.ConstraintType()
    36  	labelType := s.LabelType()
    37  
    38  	if constraintType == cue.PatternConstraint {
    39  		switch labelType {
    40  		case cue.StringLabel:
    41  			return adt.AnyString
    42  		case cue.IndexLabel:
    43  			return adt.AnyIndex
    44  
    45  		// These are not really a thing at the moment:
    46  		case cue.DefinitionLabel:
    47  			return adt.AnyDefinition
    48  		case cue.HiddenLabel:
    49  			return adt.AnyHidden // TODO: fix
    50  		case cue.HiddenDefinitionLabel:
    51  			return adt.AnyHidden // TODO: fix
    52  		default:
    53  			panic("unreachable")
    54  		}
    55  	}
    56  
    57  	switch labelType {
    58  	case cue.StringLabel:
    59  		return adt.MakeStringLabel(r, s.Unquoted())
    60  
    61  	case cue.IndexLabel:
    62  		return adt.MakeIntLabel(adt.IntLabel, int64(s.Index()))
    63  
    64  	case cue.DefinitionLabel:
    65  		return adt.MakeNamedLabel(r, adt.DefinitionLabel, s.String())
    66  
    67  	case cue.HiddenLabel:
    68  		str := adt.HiddenKey(s.String(), s.PkgPath())
    69  		return adt.MakeNamedLabel(r, adt.HiddenLabel, str)
    70  
    71  	case cue.HiddenDefinitionLabel:
    72  		str := adt.HiddenKey(s.String(), s.PkgPath())
    73  		return adt.MakeNamedLabel(r, adt.HiddenDefinitionLabel, str)
    74  
    75  	default:
    76  		return adt.InvalidLabel
    77  	}
    78  }
    79  

View as plain text