cuelang.org/go@v0.13.0/internal/filetypes/types_gen.go (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  	"cmp"
     9  	_ "embed"
    10  	"fmt"
    11  	"maps"
    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/genstruct"
    19  	"cuelang.org/go/internal/filetypes/internal/opt"
    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  		"auto":           TagTopLevel,
    35  		"binary":         TagTopLevel,
    36  		"code":           TagTopLevel,
    37  		"cue":            TagTopLevel,
    38  		"dag":            TagTopLevel,
    39  		"data":           TagTopLevel,
    40  		"go":             TagTopLevel,
    41  		"graph":          TagTopLevel,
    42  		"json":           TagTopLevel,
    43  		"jsonl":          TagTopLevel,
    44  		"jsonschema":     TagTopLevel,
    45  		"koala":          TagSubsidiaryBool,
    46  		"lang":           TagSubsidiaryString,
    47  		"openapi":        TagTopLevel,
    48  		"pb":             TagTopLevel,
    49  		"proto":          TagTopLevel,
    50  		"schema":         TagTopLevel,
    51  		"strict":         TagSubsidiaryBool,
    52  		"strictFeatures": TagSubsidiaryBool,
    53  		"strictKeywords": TagSubsidiaryBool,
    54  		"text":           TagTopLevel,
    55  		"textproto":      TagTopLevel,
    56  		"toml":           TagTopLevel,
    57  		"xml":            TagTopLevel,
    58  		"yaml":           TagTopLevel,
    59  	}
    60  }
    61  
    62  var (
    63  	allFileExts = []string{
    64  		"-",
    65  		".cue",
    66  		".go",
    67  		".json",
    68  		".jsonl",
    69  		".ldjson",
    70  		".ndjson",
    71  		".proto",
    72  		".textpb",
    73  		".textproto",
    74  		".toml",
    75  		".txt",
    76  		".wasm",
    77  		".xml",
    78  		".yaml",
    79  		".yml",
    80  		".unknown",
    81  		"",
    82  	}
    83  	allFileExts_rev = genstruct.IndexMap(allFileExts)
    84  )
    85  var (
    86  	allTopLevelTags = []string{
    87  		"auto",
    88  		"binary",
    89  		"code",
    90  		"cue",
    91  		"dag",
    92  		"data",
    93  		"go",
    94  		"graph",
    95  		"json",
    96  		"jsonl",
    97  		"jsonschema",
    98  		"openapi",
    99  		"pb",
   100  		"proto",
   101  		"schema",
   102  		"text",
   103  		"textproto",
   104  		"toml",
   105  		"xml",
   106  		"yaml",
   107  	}
   108  	allTopLevelTags_rev = genstruct.IndexMap(allTopLevelTags)
   109  )
   110  
   111  var (
   112  	allEncodings = []build.Encoding{
   113  		"binary",
   114  		"binarypb",
   115  		"code",
   116  		"cue",
   117  		"json",
   118  		"jsonl",
   119  		"proto",
   120  		"text",
   121  		"textproto",
   122  		"toml",
   123  		"xml",
   124  		"yaml",
   125  		"",
   126  	}
   127  	allEncodings_rev = genstruct.IndexMap(allEncodings)
   128  )
   129  var (
   130  	allInterpretations = []build.Interpretation{
   131  		"auto",
   132  		"jsonschema",
   133  		"openapi",
   134  		"pb",
   135  		"",
   136  	}
   137  	allInterpretations_rev = genstruct.IndexMap(allInterpretations)
   138  )
   139  var (
   140  	allForms = []build.Form{
   141  		"dag",
   142  		"data",
   143  		"final",
   144  		"graph",
   145  		"schema",
   146  		"",
   147  	}
   148  	allForms_rev = genstruct.IndexMap(allForms)
   149  )
   150  
   151  func toFileGenerated(mode Mode, sc *scope, filename string) (*build.File, errors.Error) {
   152  	key := make([]byte, 5)
   153  	genstruct.PutSet(key, 2, 3, allTopLevelTags_rev, maps.Keys(sc.topLevel))
   154  	genstruct.PutEnum(key, 1, 1, allFileExts_rev, 16, fileExt(filename))
   155  	genstruct.PutUint64(key, 0, 1, uint64(mode))
   156  
   157  	data, ok := genstruct.FindRecord(fileInfoDataBytes, 5+6, key)
   158  	if !ok {
   159  		return nil, errors.Newf(token.NoPos, "invalid tag combination") // TODO what error would be best?
   160  	}
   161  
   162  	switch e := internal.ErrorKind(genstruct.GetUint64(data, 3, 1)); e {
   163  	default:
   164  		return nil, errors.Newf(token.NoPos, "unknown filetype error %d", e)
   165  	case internal.ErrUnknownFileExtension:
   166  		return nil, errors.Newf(token.NoPos, "unknown file extension %s", fileExt(filename))
   167  	case internal.ErrCouldNotDetermineFileType:
   168  		return nil, errors.Newf(token.NoPos, "could not determine file type for file %q", filename)
   169  	case internal.ErrNoEncodingSpecified:
   170  		return nil, errors.Newf(token.NoPos, "no encoding specified for file %q", filename)
   171  	case 0:
   172  		// no error
   173  	}
   174  
   175  	var f build.File
   176  	f.Filename = filename
   177  	f.Encoding = genstruct.GetEnum(data, 0, 1, allEncodings)
   178  	f.Interpretation = genstruct.GetEnum(data, 1, 1, allInterpretations)
   179  	f.Form = genstruct.GetEnum(data, 2, 1, allForms)
   180  	if index := int(genstruct.GetUint64(data, 4, 1)); index > 0 {
   181  		tagFunc := subsidiaryTagFuncs[index-1]
   182  		var t subsidiaryTags
   183  		if err := t.unmarshalFromMap(sc.subsidiaryString); err != nil {
   184  			return nil, errors.Promote(err, "")
   185  		}
   186  		t, err := tagFunc(t)
   187  		if err != nil {
   188  			return nil, errors.Promote(err, "")
   189  		}
   190  		f.Tags = t.marshalToMap()
   191  
   192  	} else if len(sc.subsidiaryString) > 0 {
   193  		return nil, errors.Newf(token.NoPos, "tag %s is not allowed in this context", someKey(sc.subsidiaryString))
   194  	}
   195  	if index := int(genstruct.GetUint64(data, 5, 1)); index > 0 {
   196  		tagFunc := subsidiaryBoolTagFuncs[index-1]
   197  		var t subsidiaryBoolTags
   198  		if err := t.unmarshalFromMap(sc.subsidiaryBool); err != nil {
   199  			return nil, errors.Promote(err, "")
   200  		}
   201  		t, err := tagFunc(t)
   202  		if err != nil {
   203  			return nil, errors.Promote(err, "")
   204  		}
   205  		f.BoolTags = t.marshalToMap()
   206  	} else if len(sc.subsidiaryBool) > 0 {
   207  		return nil, errors.Newf(token.NoPos, "tag %s is not allowed in this context", someKey(sc.subsidiaryBool))
   208  	}
   209  
   210  	return &f, nil
   211  }
   212  
   213  func fromFileGenerated(b *build.File, mode Mode) (*FileInfo, error) {
   214  	key := make([]byte, 4)
   215  	genstruct.PutUint64(key, 0, 1, uint64(mode))
   216  	genstruct.PutEnum(key, 1, 1, allEncodings_rev, 12, b.Encoding)
   217  	genstruct.PutEnum(key, 2, 1, allInterpretations_rev, 4, b.Interpretation)
   218  	genstruct.PutEnum(key, 3, 1, allForms_rev, 5, b.Form)
   219  
   220  	data, ok := genstruct.FindRecord(fromFileDataBytes, 4+5, key)
   221  	if !ok {
   222  		return nil, errors.Newf(token.NoPos, "no encoding specified")
   223  	}
   224  	fi := &FileInfo{
   225  		Filename:       b.Filename,
   226  		Encoding:       genstruct.GetEnum(data, 0, 1, allEncodings),
   227  		Interpretation: genstruct.GetEnum(data, 1, 1, allInterpretations),
   228  		Form:           genstruct.GetEnum(data, 2, 1, allForms),
   229  	}
   230  	fi.SetAspects(internal.Aspects(genstruct.GetUint64(data, 3, 2)))
   231  	return fi, nil
   232  }
   233  
   234  func someKey[K cmp.Ordered, V any](m map[K]V) K {
   235  	return slices.Sorted(maps.Keys(m))[0]
   236  }
   237  
   238  var subsidiaryBoolTagFuncs = []func(subsidiaryBoolTags) (subsidiaryBoolTags, error){
   239  	unifySubsidiaryBoolTags_0,
   240  	unifySubsidiaryBoolTags_1,
   241  	unifySubsidiaryBoolTags_2,
   242  }
   243  
   244  var subsidiaryTagFuncs = []func(subsidiaryTags) (subsidiaryTags, error){
   245  	unifySubsidiaryTags_0,
   246  	unifySubsidiaryTags_1,
   247  	unifySubsidiaryTags_2,
   248  }
   249  
   250  type subsidiaryTags struct {
   251  	lang opt.Opt[string]
   252  }
   253  
   254  func (t *subsidiaryTags) unmarshalFromMap(m map[string]string) error {
   255  	if x, ok := m["lang"]; ok {
   256  		t.lang = opt.Some(x)
   257  	}
   258  	return nil
   259  }
   260  func (t subsidiaryTags) marshalToMap() map[string]string {
   261  	m := make(map[string]string)
   262  	if t.lang.IsPresent() {
   263  		m["lang"] = t.lang.Value()
   264  	}
   265  	return m
   266  }
   267  
   268  type subsidiaryBoolTags struct {
   269  	koala          opt.Opt[bool]
   270  	strict         opt.Opt[bool]
   271  	strictFeatures opt.Opt[bool]
   272  	strictKeywords opt.Opt[bool]
   273  }
   274  
   275  func (t *subsidiaryBoolTags) unmarshalFromMap(m map[string]bool) error {
   276  	if x, ok := m["koala"]; ok {
   277  		t.koala = opt.Some(x)
   278  	}
   279  	if x, ok := m["strict"]; ok {
   280  		t.strict = opt.Some(x)
   281  	}
   282  	if x, ok := m["strictFeatures"]; ok {
   283  		t.strictFeatures = opt.Some(x)
   284  	}
   285  	if x, ok := m["strictKeywords"]; ok {
   286  		t.strictKeywords = opt.Some(x)
   287  	}
   288  	return nil
   289  }
   290  func (t subsidiaryBoolTags) marshalToMap() map[string]bool {
   291  	m := make(map[string]bool)
   292  	if t.koala.IsPresent() {
   293  		m["koala"] = t.koala.Value()
   294  	}
   295  	if t.strict.IsPresent() {
   296  		m["strict"] = t.strict.Value()
   297  	}
   298  	if t.strictFeatures.IsPresent() {
   299  		m["strictFeatures"] = t.strictFeatures.Value()
   300  	}
   301  	if t.strictKeywords.IsPresent() {
   302  		m["strictKeywords"] = t.strictKeywords.Value()
   303  	}
   304  	return m
   305  }
   306  
   307  // unifySubsidiaryTags_0 unifies subsidiaryTags values according to the following CUE logic:
   308  //
   309  //	{
   310  //		{
   311  //			[string]: string
   312  //		}
   313  //		lang: "go"
   314  //	}
   315  func unifySubsidiaryTags_0(t subsidiaryTags) (subsidiaryTags, error) {
   316  	var r subsidiaryTags
   317  	r.lang = opt.Some("go")
   318  	if t.lang.IsPresent() && t.lang.Value() != r.lang.Value() {
   319  		return subsidiaryTags{}, fmt.Errorf("conflict on lang; %#v provided but need %#v", t.lang.Value(), r.lang.Value())
   320  	}
   321  	return r, nil
   322  }
   323  
   324  // unifySubsidiaryTags_2 unifies subsidiaryTags values according to the following CUE logic:
   325  //
   326  //	{
   327  //		{
   328  //			[string]: string
   329  //		}
   330  //		lang: (*"" | string) & {
   331  //			"go"
   332  //		}
   333  //	}
   334  func unifySubsidiaryTags_2(t subsidiaryTags) (subsidiaryTags, error) {
   335  	var r subsidiaryTags
   336  	r.lang = opt.Some("go")
   337  	if t.lang.IsPresent() && t.lang.Value() != r.lang.Value() {
   338  		return subsidiaryTags{}, fmt.Errorf("conflict on lang; %#v provided but need %#v", t.lang.Value(), r.lang.Value())
   339  	}
   340  	return r, nil
   341  }
   342  
   343  // unifySubsidiaryTags_1 unifies subsidiaryTags values according to the following CUE logic:
   344  //
   345  //	{
   346  //		{
   347  //			[string]: string
   348  //		}
   349  //		lang: *"" | string
   350  //	}
   351  func unifySubsidiaryTags_1(t subsidiaryTags) (subsidiaryTags, error) {
   352  	var r subsidiaryTags
   353  	r.lang = opt.Some("")
   354  	if t.lang.IsPresent() {
   355  		r.lang = t.lang
   356  	}
   357  	return r, nil
   358  }
   359  
   360  // unifySubsidiaryBoolTags_2 unifies subsidiaryBoolTags values according to the following CUE logic:
   361  //
   362  //	{
   363  //		{
   364  //			[string]: bool
   365  //		}
   366  //		koala:          *false | bool
   367  //		strict:         *false | bool
   368  //		strictKeywords: *strict | bool
   369  //		strictFeatures: *strict | bool
   370  //	}
   371  func unifySubsidiaryBoolTags_2(t subsidiaryBoolTags) (subsidiaryBoolTags, error) {
   372  	var r subsidiaryBoolTags
   373  	r.koala = opt.Some(false)
   374  	if t.koala.IsPresent() {
   375  		r.koala = t.koala
   376  	}
   377  	r.strict = opt.Some(false)
   378  	if t.strict.IsPresent() {
   379  		r.strict = t.strict
   380  	}
   381  	r.strictFeatures = r.strict
   382  	if t.strictFeatures.IsPresent() {
   383  		r.strictFeatures = t.strictFeatures
   384  	}
   385  	r.strictKeywords = r.strict
   386  	if t.strictKeywords.IsPresent() {
   387  		r.strictKeywords = t.strictKeywords
   388  	}
   389  	return r, nil
   390  }
   391  
   392  // unifySubsidiaryBoolTags_0 unifies subsidiaryBoolTags values according to the following CUE logic:
   393  //
   394  //	{
   395  //		{
   396  //			[string]: bool
   397  //		}
   398  //		koala: *false | bool
   399  //	}
   400  func unifySubsidiaryBoolTags_0(t subsidiaryBoolTags) (subsidiaryBoolTags, error) {
   401  	var r subsidiaryBoolTags
   402  	r.koala = opt.Some(false)
   403  	if t.koala.IsPresent() {
   404  		r.koala = t.koala
   405  	}
   406  	if t.strict.IsPresent() {
   407  		return subsidiaryBoolTags{}, fmt.Errorf("field %q not allowed", "strict")
   408  	}
   409  	if t.strictFeatures.IsPresent() {
   410  		return subsidiaryBoolTags{}, fmt.Errorf("field %q not allowed", "strictFeatures")
   411  	}
   412  	if t.strictKeywords.IsPresent() {
   413  		return subsidiaryBoolTags{}, fmt.Errorf("field %q not allowed", "strictKeywords")
   414  	}
   415  	return r, nil
   416  }
   417  
   418  // unifySubsidiaryBoolTags_1 unifies subsidiaryBoolTags values according to the following CUE logic:
   419  //
   420  //	{
   421  //		{
   422  //			[string]: bool
   423  //		}
   424  //		strict:         *false | bool
   425  //		strictKeywords: *strict | bool
   426  //		strictFeatures: *strict | bool
   427  //	}
   428  func unifySubsidiaryBoolTags_1(t subsidiaryBoolTags) (subsidiaryBoolTags, error) {
   429  	var r subsidiaryBoolTags
   430  	if t.koala.IsPresent() {
   431  		return subsidiaryBoolTags{}, fmt.Errorf("field %q not allowed", "koala")
   432  	}
   433  	r.strict = opt.Some(false)
   434  	if t.strict.IsPresent() {
   435  		r.strict = t.strict
   436  	}
   437  	r.strictFeatures = r.strict
   438  	if t.strictFeatures.IsPresent() {
   439  		r.strictFeatures = t.strictFeatures
   440  	}
   441  	r.strictKeywords = r.strict
   442  	if t.strictKeywords.IsPresent() {
   443  		r.strictKeywords = t.strictKeywords
   444  	}
   445  	return r, nil
   446  }