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