cuelang.org/go@v0.10.1/internal/filetypes/types.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 filetypes 16 17 import ( 18 _ "embed" 19 "fmt" 20 "sync" 21 22 "cuelang.org/go/cue" 23 "cuelang.org/go/cue/build" 24 "cuelang.org/go/cue/cuecontext" 25 ) 26 27 //go:embed types.cue 28 var typesCUE string 29 30 var ( 31 typesValue cue.Value 32 fileForExt map[string]*build.File 33 fileForCUE *build.File 34 ) 35 36 var typesInit = sync.OnceFunc(func() { 37 ctx := cuecontext.New() 38 typesValue = ctx.CompileString(typesCUE, cue.Filename("types.cue")) 39 if err := typesValue.Err(); err != nil { 40 panic(err) 41 } 42 // Reading a file in input mode with a non-explicit scope is a very 43 // common operation, so cache the build.File value for all 44 // the known file extensions. 45 if err := typesValue.LookupPath(cue.MakePath(cue.Str("fileForExtVanilla"))).Decode(&fileForExt); err != nil { 46 panic(err) 47 } 48 fileForCUE = fileForExt[".cue"] 49 // Check invariants assumed by FromFile 50 if fileForCUE.Form != "" || fileForCUE.Interpretation != "" || fileForCUE.Encoding != build.CUE { 51 panic(fmt.Errorf("unexpected value for CUE file type: %#v", fileForCUE)) 52 } 53 })