cuelang.org/go@v0.13.0/internal/filetypes/types.cue (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 build
    16  
    17  // This file describes how various cross-cutting modes influence default
    18  // settings.
    19  //
    20  // It is used by types.go to compile a cue.Value, which is then
    21  // used by the rest of the package to determine settings.
    22  
    23  // A File corresponds to a Go build.File.
    24  #File: {
    25  	filename?:       string // only filled in FromFile, but not in ParseFile
    26  	encoding!:       #Encoding
    27  	interpretation?: #Interpretation
    28  	form?:           #Form
    29  	// Note: tags includes values for non-boolean tags only.
    30  	tags?: [string]:     string
    31  	boolTags?: [string]: bool
    32  }
    33  
    34  // A FileInfo defines how a file is encoded and interpreted.
    35  #FileInfo: {
    36  	#File
    37  
    38  	// For each of these fields it is explained what a true value means
    39  	// for encoding/decoding.
    40  
    41  	data:          *true | false // include/allow regular fields
    42  	references:    *true | false // don't resolve/allow references
    43  	cycles:        *true | false // cycles are permitted
    44  	definitions?:  bool          // include/allow definition fields
    45  	optional?:     bool          // include/allow definition fields
    46  	constraints?:  bool          // include/allow constraints
    47  	keepDefaults?: bool          // select/allow default values
    48  	incomplete?:   bool          // permit incomplete values
    49  	imports?:      bool          // don't expand/allow imports
    50  	stream?:       bool          // permit streaming
    51  	docs?:         bool          // show/allow docs
    52  	attributes?:   bool          // include/allow attributes
    53  }
    54  
    55  // fileForExtVanilla holds the extensions supported in
    56  // input mode with scope="" - the most common form
    57  // of file type to evaluate.
    58  //
    59  // It's also used as a source of truth for all known file
    60  // extensions as all modes define attributes for
    61  // all file extensions. If that ever changed, we'd need
    62  // to change this.
    63  fileForExtVanilla: modes.input.extensions
    64  
    65  // #Mode represents an overall mode that CUE is being run in
    66  // (e.g. eval, export).
    67  #Mode: {
    68  	// FileInfo holds the base file information for this mode.
    69  	// This will be unified with information derived from the
    70  	// file extension and any filetype tags explicitly provided.
    71  	FileInfo!: #FileInfo
    72  
    73  	// extensions holds the set of file extensions that are defined
    74  	// for this mode.
    75  	extensions!: [_]: #FileInfo
    76  
    77  	// encodings holds the set of encodings defined for this mode.
    78  	encodings!: [_]: #FileInfo
    79  }
    80  
    81  // modes sets defaults for different operational modes.
    82  // The key corresponds to the Go internal/filetypes.Mode type.
    83  modes: [string]: #Mode
    84  
    85  // input defines modes for input, such as import, eval, vet or def.
    86  // In input mode, settings flags are interpreted as what is allowed to occur
    87  // in the input. The default settings, therefore, tend to be permissive.
    88  modes: input: {
    89  	// FileInfo holds a value that's unified with the file value.
    90  	FileInfo: {
    91  		docs:       *true | false
    92  		attributes: *true | false
    93  	}
    94  	// encodings is unified to the file value when interpretation is empty.
    95  	encodings: cue: {
    96  		*forms.schema | _
    97  	}
    98  	extensions: "-": encoding:           *"cue" | _
    99  	extensions: ".json": interpretation: *"auto" | _
   100  	extensions: ".yaml": interpretation: *"auto" | _
   101  	extensions: ".yml": interpretation:  *"auto" | _
   102  	extensions: ".toml": interpretation: *"auto" | _
   103  	extensions: ".xml": interpretation:  *"auto" | _
   104  }
   105  
   106  modes: export: {
   107  	FileInfo: {
   108  		docs:       true | *false
   109  		attributes: true | *false
   110  	}
   111  	encodings: cue: forms.data
   112  	extensions: "-": encoding: *"json" | _
   113  }
   114  
   115  // eval is a legacy mode
   116  modes: eval: {
   117  	FileInfo: {
   118  		docs:       true | *false
   119  		attributes: true | *false
   120  	}
   121  	encodings: cue: forms.final
   122  	extensions: "-": encoding: *"cue" | _
   123  }
   124  
   125  modes: def: {
   126  	FileInfo: {
   127  		docs:       *true | false
   128  		attributes: *true | false
   129  	}
   130  	encodings: cue: forms.schema
   131  	extensions: "-": encoding: *"cue" | _
   132  }
   133  
   134  // A Encoding indicates a file format for representing a program.
   135  #Encoding: !="" // | error("no encoding specified")
   136  
   137  // An Interpretation determines how a certain program should be interpreted.
   138  // For instance, data may be interpreted as describing a schema, which itself
   139  // can be converted to a CUE schema.
   140  // This corresponds to the Go cue/build.Interpretation type.
   141  #Interpretation: string
   142  
   143  // A Form specifies the form in which a program should be represented.
   144  // It corresponds to the Go cue/build.Form type.
   145  #Form: string
   146  
   147  all: {
   148  	for m in modes {
   149  		for name, _ in m.encodings {
   150  			encodings: (name): true
   151  		}
   152  		for name, _ in m.extensions {
   153  			extensions: (name): true
   154  		}
   155  	}
   156  	for name, _ in interpretations {
   157  		interpretations: (name): true
   158  	}
   159  	for name, _ in forms {
   160  		forms: (name): true
   161  	}
   162  }
   163  
   164  modes: [string]: {
   165  	// extensions maps a file extension to its associated default file properties.
   166  	extensions: {
   167  		".cue":       tagInfo.cue
   168  		".json":      tagInfo.json
   169  		".jsonl":     tagInfo.jsonl
   170  		".ldjson":    tagInfo.jsonl
   171  		".ndjson":    tagInfo.jsonl
   172  		".yaml":      tagInfo.yaml
   173  		".yml":       tagInfo.yaml
   174  		".toml":      tagInfo.toml
   175  		".xml":       tagInfo.xml
   176  		".txt":       tagInfo.text
   177  		".go":        tagInfo.go
   178  		".wasm":      tagInfo.binary
   179  		".proto":     tagInfo.proto
   180  		".textproto": tagInfo.textproto
   181  		".textpb":    tagInfo.textproto // perhaps also pbtxt
   182  
   183  		// TODO: jsonseq,
   184  		// ".pb":        tagInfo.binpb // binarypb
   185  	}
   186  
   187  	// encodings: "": error("no encoding specified")
   188  
   189  	encodings: cue: {
   190  		stream: false
   191  	}
   192  
   193  	encodings: json: {
   194  		forms.data
   195  		stream:     *false | true
   196  		docs:       false
   197  		attributes: false
   198  	}
   199  
   200  	encodings: yaml: {
   201  		forms.graph
   202  		stream: false | *true
   203  	}
   204  
   205  	encodings: jsonl: {
   206  		forms.data
   207  		stream: true
   208  	}
   209  
   210  	encodings: text: {
   211  		forms.data
   212  		stream: false
   213  	}
   214  
   215  	encodings: binary: {
   216  		forms.data
   217  		stream: false
   218  	}
   219  
   220  	encodings: toml: {
   221  		forms.data
   222  		stream: false
   223  	}
   224  
   225  	encodings: xml: {
   226  		forms.data
   227  		stream: false
   228  	}
   229  
   230  	encodings: proto: {
   231  		forms.schema
   232  		encoding: "proto"
   233  	}
   234  
   235  	encodings: textproto: {
   236  		forms.data
   237  		encoding: "textproto"
   238  		stream:   false
   239  	}
   240  
   241  	encodings: binarypb: {
   242  		forms.data
   243  		encoding: "binarypb"
   244  		stream:   false
   245  	}
   246  
   247  	encodings: code: {
   248  		forms.schema
   249  		stream: false
   250  	}
   251  }
   252  
   253  // forms defines schema for all forms. It does not include the form ID.
   254  forms: [Name=string]: #FileInfo
   255  
   256  forms: schema: {
   257  	form: *"schema" | "final" | "graph"
   258  
   259  	stream:       true | *false
   260  	incomplete:   *true | false
   261  	definitions:  *true | false
   262  	optional:     *true | false
   263  	constraints:  *true | false
   264  	keepDefaults: *true | false
   265  	imports:      *true | false
   266  }
   267  
   268  forms: final: {
   269  	form: "final"
   270  	forms.schema
   271  
   272  	keepDefaults: false
   273  	optional:     false
   274  }
   275  
   276  forms: graph: {
   277  	form: *"graph" | "dag" | "data"
   278  	data: true
   279  
   280  	incomplete:   false
   281  	definitions:  false
   282  	optional:     false
   283  	constraints:  false
   284  	keepDefaults: false
   285  	imports:      false
   286  }
   287  
   288  forms: dag: {
   289  	form: !="graph"
   290  	forms.graph
   291  
   292  	cycles: false
   293  }
   294  
   295  forms: data: {
   296  	form: !="dag"
   297  	forms.dag
   298  
   299  	constraints: false
   300  	references:  false
   301  	cycles:      false
   302  	imports:     false
   303  	optional:    false
   304  }
   305  
   306  interpretations: [Name=string]: #FileInfo & {
   307  	interpretation: Name
   308  }
   309  
   310  interpretations: auto: forms.schema
   311  
   312  interpretations: jsonschema: {
   313  	forms.schema
   314  	encoding: *"json" | _
   315  	boolTags: {
   316  		strict:         *false | bool
   317  		strictKeywords: *strict | bool
   318  		// TODO: enable strictFeatures by default? (see https://cuelang.org/issue/3923).
   319  		strictFeatures: *strict | bool
   320  	}
   321  }
   322  
   323  interpretations: openapi: {
   324  	forms.schema
   325  	encoding: *"json" | _
   326  	boolTags: {
   327  		strict:         *false | bool
   328  		strictKeywords: *strict | bool
   329  		strictFeatures: *strict | bool
   330  	}
   331  }
   332  
   333  interpretations: pb: {
   334  	forms.data
   335  	stream: true
   336  }
   337  
   338  tagInfo: [_]: #FileInfo
   339  
   340  // tagInfo maps command line tags to file properties.
   341  tagInfo: {
   342  	schema: form: "schema"
   343  	graph: form:  "graph"
   344  	dag: form:    "dag"
   345  	data: form:   "data"
   346  
   347  	cue: encoding:   "cue"
   348  	json: encoding:  "json"
   349  	jsonl: encoding: "jsonl"
   350  	yaml: encoding:  "yaml"
   351  	toml: encoding:  "toml"
   352  	xml: {
   353  		encoding: "xml"
   354  		boolTags: {
   355  			// We implement XML variants as boolean tags, such that "koala" is not accessible
   356  			// as a top-level filetype, and can only be used via "xml+koala".
   357  			// These effectively behave like a "one of", but we enforce this via the Go code
   358  			// so that we can provide the users with good error messages.
   359  			koala: *false | bool
   360  		}
   361  	}
   362  	proto: encoding:     "proto"
   363  	textproto: encoding: "textproto"
   364  	// "binpb":  encodings.binproto
   365  
   366  	// pb is used either to indicate binary encoding, or to indicate
   367  	pb: *{
   368  		encoding:       "binarypb"
   369  		interpretation: ""
   370  	} | {
   371  		encoding:       !="binarypb"
   372  		interpretation: "pb"
   373  	}
   374  
   375  	text: {
   376  		encoding: "text"
   377  		form:     "data"
   378  	}
   379  	binary: {
   380  		encoding: "binary"
   381  		form:     "data"
   382  	}
   383  	go: {
   384  		encoding:       "code"
   385  		interpretation: ""
   386  		tags: lang: "go"
   387  	}
   388  	code: {
   389  		encoding:       "code"
   390  		interpretation: ""
   391  		tags: lang: *"" | string
   392  	}
   393  	auto: interpretations.auto & {
   394  		encoding: *"json" | string
   395  	}
   396  	jsonschema: interpretations.jsonschema
   397  	openapi:    interpretations.openapi
   398  }