cuelang.org/go@v0.13.0/internal/filetypes/types_gen.go.tmpl (about) 1 // Code generated by cuelang.org/go/pkg/gen. DO NOT EDIT. 2 3 //go:build !bootstrap 4 5 package filetypes 6 7 import ( 8 _ "embed" 9 "cmp" 10 "maps" 11 "fmt" 12 "slices" 13 14 "cuelang.org/go/cue/build" 15 "cuelang.org/go/cue/errors" 16 "cuelang.org/go/cue/token" 17 "cuelang.org/go/internal/filetypes/internal" 18 "cuelang.org/go/internal/filetypes/internal/opt" 19 "cuelang.org/go/internal/filetypes/internal/genstruct" 20 ) 21 22 // TODO use string instead of []byte so that we can 23 // use the init data directly without copying into 24 // read-write memory. 25 26 //go:embed fileinfo.dat 27 var fileInfoDataBytes []byte 28 29 //go:embed fromfile.dat 30 var fromFileDataBytes []byte 31 32 func init() { 33 tagTypes = map[string]TagType{ 34 {{range $k, $v := .TagTypes}}{{printf "\t%q: %v,\n" $k $v}}{{end}} 35 } 36 } 37 38 {{.ToFileParams.GenInit .Generated}} 39 40 {{.ToFileResult.GenInit .Generated}} 41 42 {{.FromFileParams.GenInit .Generated}} 43 44 {{.FromFileResult.GenInit .Generated}} 45 46 func toFileGenerated(mode Mode, sc *scope, filename string) (*build.File, errors.Error) { 47 key := make([]byte, {{.ToFileParams.Size}}) 48 {{.ToFileParams.Tags.GenPut "key" "maps.Keys(sc.topLevel)"}} 49 {{.ToFileParams.FileExt.GenPut "key" "fileExt(filename)"}} 50 {{.ToFileParams.Mode.GenPut "key" "mode"}} 51 52 data, ok := genstruct.FindRecord(fileInfoDataBytes, {{.ToFileParams.Size}}+{{.ToFileResult.Size}}, key) 53 if !ok { 54 return nil, errors.Newf(token.NoPos, "invalid tag combination") // TODO what error would be best? 55 } 56 57 switch e := {{.ToFileResult.Error.GenGet "data"}}; e { 58 default: 59 return nil, errors.Newf(token.NoPos, "unknown filetype error %d", e) 60 case internal.ErrUnknownFileExtension: 61 return nil, errors.Newf(token.NoPos, "unknown file extension %s", fileExt(filename)) 62 case internal.ErrCouldNotDetermineFileType: 63 return nil, errors.Newf(token.NoPos, "could not determine file type for file %q", filename) 64 case internal.ErrNoEncodingSpecified: 65 return nil, errors.Newf(token.NoPos, "no encoding specified for file %q", filename) 66 case 0: 67 // no error 68 } 69 70 var f build.File 71 f.Filename = filename 72 f.Encoding = {{.ToFileResult.Encoding.GenGet "data"}} 73 f.Interpretation = {{.ToFileResult.Interpretation.GenGet "data"}} 74 f.Form = {{.ToFileResult.Form.GenGet "data"}} 75 if index := {{.ToFileResult.SubsidiaryTagFuncIndex.GenGet "data"}}; index > 0 { 76 tagFunc := subsidiaryTagFuncs[index-1] 77 var t subsidiaryTags 78 if err := t.unmarshalFromMap(sc.subsidiaryString); err != nil { 79 return nil, errors.Promote(err, "") 80 } 81 t, err := tagFunc(t) 82 if err != nil { 83 return nil, errors.Promote(err, "") 84 } 85 f.Tags = t.marshalToMap() 86 87 } else if len(sc.subsidiaryString) > 0 { 88 return nil, errors.Newf(token.NoPos, "tag %s is not allowed in this context", someKey(sc.subsidiaryString)) 89 } 90 if index := {{.ToFileResult.SubsidiaryBoolTagFuncIndex.GenGet "data"}}; index > 0 { 91 tagFunc := subsidiaryBoolTagFuncs[index-1] 92 var t subsidiaryBoolTags 93 if err := t.unmarshalFromMap(sc.subsidiaryBool); err != nil { 94 return nil, errors.Promote(err, "") 95 } 96 t, err := tagFunc(t) 97 if err != nil { 98 return nil, errors.Promote(err, "") 99 } 100 f.BoolTags = t.marshalToMap() 101 } else if len(sc.subsidiaryBool) > 0 { 102 return nil, errors.Newf(token.NoPos, "tag %s is not allowed in this context", someKey(sc.subsidiaryBool)) 103 } 104 105 return &f, nil 106 } 107 108 func fromFileGenerated(b *build.File, mode Mode) (*FileInfo, error) { 109 key := make([]byte, {{.FromFileParams.Size}}) 110 {{.FromFileParams.Mode.GenPut "key" "mode"}} 111 {{.FromFileParams.Encoding.GenPut "key" "b.Encoding"}} 112 {{.FromFileParams.Interpretation.GenPut "key" "b.Interpretation"}} 113 {{.FromFileParams.Form.GenPut "key" "b.Form"}} 114 115 data, ok := genstruct.FindRecord(fromFileDataBytes, {{.FromFileParams.Size}}+{{.FromFileResult.Size}}, key) 116 if !ok { 117 return nil, errors.Newf(token.NoPos, "no encoding specified") 118 } 119 fi := &FileInfo{ 120 Filename: b.Filename, 121 Encoding: {{.FromFileResult.Encoding.GenGet "data"}}, 122 Interpretation: {{.FromFileResult.Interpretation.GenGet "data"}}, 123 Form: {{.FromFileResult.Form.GenGet "data"}}, 124 } 125 fi.SetAspects({{.FromFileResult.Aspects.GenGet "data"}}) 126 return fi, nil 127 } 128 129 func someKey[K cmp.Ordered, V any](m map[K] V) K { 130 return slices.Sorted(maps.Keys(m))[0] 131 } 132 133 var subsidiaryBoolTagFuncs = []func(subsidiaryBoolTags) (subsidiaryBoolTags, error) { 134 {{range .SubsidiaryBoolTagFuncCount}}unifySubsidiaryBoolTags_{{.}}, 135 {{end}} 136 } 137 138 var subsidiaryTagFuncs = []func(subsidiaryTags) (subsidiaryTags, error) { 139 {{range .SubsidiaryTagFuncCount}}unifySubsidiaryTags_{{.}}, 140 {{end}} 141 }