github.com/solo-io/cue@v0.4.7/internal/core/export/label.go (about) 1 // Copyright 2020 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 export 16 17 import ( 18 "strconv" 19 "strings" 20 21 "github.com/solo-io/cue/cue/ast" 22 "github.com/solo-io/cue/cue/literal" 23 "github.com/solo-io/cue/cue/token" 24 "github.com/solo-io/cue/internal/core/adt" 25 ) 26 27 func (e *exporter) stringLabel(f adt.Feature) ast.Label { 28 x := f.Index() 29 switch f.Typ() { 30 case adt.IntLabel: 31 return ast.NewLit(token.INT, strconv.Itoa(int(x))) 32 33 case adt.DefinitionLabel, adt.HiddenLabel, adt.HiddenDefinitionLabel: 34 s := f.IdentString(e.ctx) 35 return ast.NewIdent(s) 36 37 case adt.StringLabel: 38 s := e.ctx.IndexToString(int64(x)) 39 if f == 0 || !ast.IsValidIdent(s) || 40 strings.HasPrefix(s, "#") || strings.HasPrefix(s, "_") { 41 return ast.NewLit(token.STRING, literal.Label.Quote(s)) 42 } 43 fallthrough 44 45 default: 46 return ast.NewIdent(e.ctx.IndexToString(int64(x))) 47 } 48 }