In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is CUE". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is CUE?
C (Configure: configuration), U (Unify: unified), E (Execute: execution).
CUE is an open source data constraint language designed to simplify tasks that involve defining and using data.
It is a superset of JSON and allows users who are familiar with JSON to get started quickly.
In other words, it is similar to JSON, YAML, etc., but more powerful than them, and can be compared with JSON, YAML and other tools to understand.
What can CUE be used for?
We can use CUE:
Define a detailed validation mode
Reduce templates in data
Extract patterns from code
Generate type definition and validation code
Merge JSON in a principled way
Define and run declarative scripts
How
CUE combines the concepts of schema and data. The same CUE definition can be used both to validate data and to reduce templates. Schema definitions are enriched through fine-grained value definitions and default values. At the same time, the data can be simplified by removing the values implied in these detailed definitions. The combination of these two concepts enables many tasks to be handled in a principled way.
Constraints provide a simple, well-defined, but powerful alternative to inheritance, which is a common source of complexity in configuration languages.
CUE script
The CUE script layer defines declarative scripts, expressed in CUE, on top of the data. This solves three problems: it solves the closeness of the CUE definition (we say CUE is sealed), provides an easy way to share common scripts and workflows that consume data, and lets CUE know how to use data to optimize validation.
There are many tools that can interpret data or use specialized languages (Kustomize, Ksonnet) for specific areas. This solves the problem of processing data at one level, but the problem it solves may be repeated at a higher level when integrating other systems in the workflow. CUE scripts are generic and allow users to define any workflow.
Installation
In the case of a Mac environment, execute the following command to install:
Brew install cuelang/tap/cue
Other environments are installed through golang (of course, mac is also possible)
# go 1.16 before GO111MODULE=on go get cuelang.org/go/cmd/cue# go 1.16 and after go install cuelang.org/go/cmd/cue@latest
For details, please refer to the installation documentation.
After installation, you can view the basic help documentation by executing cue-- help on the command line.
Cue-- helpcue evaluates CUE files, an extension of JSON, and sends themto user-defined commands for processing.Commands are defined in CUE as follows: import "tool/exec" command: deploy: {exec.Run cmd: "kubectl" args: ["- f", "deploy"] in: json.Encode (userValue) / / encode the emitted configuration. } cue can also combine the results of http or grpc request with the inputconfiguration for further processing. For more information on defining commandsrun 'cue help cmd' or go to cuelang.org/pkg/cmd.For more information on writing CUE configuration files see cuelang.org.Usage: cue [command] Available Commands: cmd run a user-defined shell command completion Generate completion script def print consolidated definitions eval evaluate and print a configuration export output data in a standard format fix rewrite packages to latest standards fmt formats CUE configuration files get add dependencies to the current module help Help about any command import convert other formats to CUE files mod module maintenance trim remove superfluous fields version print CUE version vet validate dataFlags:-E -- all-errors print all available errors-h,-- help help for cue-I,-- ignore proceed in the presence of errors-s,-- simplify simplify output-- strict report errors for lossy mappings-- trace trace computation-v Verbose print information about progressAdditional help topics: cue commands user-defined commands cue filetypes supported filetypes and qualifiers cue flags common flags for composing packages cue injection inject files or values into specific fields for a build cue inputs package list, patterns, and filesUse "cue [command]-- help" for more information about a command.
Here are some concrete examples to see how to use CUE.
JSON superset
CUE is a superset of JSON. It adds the following convenience.
C style comments.
Quotation marks can be omitted from the field name and have no special characters.
The comma at the end of the field is optional.
The comma after the last element in the list is allowed.
The outer braces are optional.
JSON objects are called structures in CUE. A member of an object is called a field.
Suppose we have the following json.cue file:
One: 1two: 2 one / A field using quotes. "two-and-a-half": 2.5list: [1,2,3] m: {key1: "v1" key2: "v2"}
You can look at the generated json data through cue export json.cue:
{"one": 1, "two": 2, "two-and-a-half": 2.5, "list": [1,2,3], "m": {"key1": "v1", "key2": "v2"}}
Cue export can convert cue files into json, yaml, text and other types of files.
Transformed into json cue export json.cue-- out json
Transformed into yaml cue export json.cue-- out yaml
Transformed into text cue export json.cue-- out text
Export to file cue export json.cue-- out json-- outfile json.cue.json
You can view detailed help documentation through cue help export
Type is value
CUE combines the concepts of values and types. The following is a demonstration of this demo, showing some data, the possible patterns of this data, and something in between: a typical CUE constraint.
Data
Moscow: {name: "Moscow" pop: 11.92m capital: true}
Schema
Municipality: {name: string pop: int capital: bool}
CUE:
LargeCapital: {name: string pop: > 5m capital: true}
In general, in CUE, people start with a broad schema definition, describe all possible instances, and then narrow the scope of these definitions for specific use cases until a specific data instance is left.
Duplicate field
CUE allows duplicate field definitions, as long as they do not conflict.
For values of primitive types, this means that they must be equal.
For structures, fields are merged and duplicate fields are recursively processed.
For lists, all elements must match accordingly
The hypothetical dup.cue is as follows:
A: 4a: 4s: {b: 2} s: {c: 2} l: [1, 2] l: [1, 2]
The merged content can be obtained through cue eval dup.cue:
A: 4s: {b: 2 c: 2} l: [1, 2]
You can also use the export command to generate json/yaml and other files to take a look.
If the key is the same, but the value is different, what will happen? modify the contents of the dup.cue file as follows:
A: 4a: 5s: {b: 2} s: {c: 2} l: [1, 2] l: [1, 3]
If you execute cue eval dup.cue again, you will get an error:
A: conflicting values 5 and 4:. / dup.cue:1:4. / dup.cue:2:4l.1: conflicting values 3 and 2:. / dup.cue:7:9. / dup.cue:8:9 restriction
The constraint specifies which values are allowed. To CUE, they are just values like everything else, but conceptually, they can be interpreted as something between a type and a specific value.
But constraints can also reduce templates. If a constraint defines a specific value, it is not necessary to specify it in the value to which the constraint applies.
Suppose the check.cue file is as follows:
Schema: {name: string age: int human: true / / always true} viola: schemaviola: {name: "Viola" age: 38}
The meaning of this document is as follows:
Schema defines constraints. Human can only be true, and cannot be changed to other values when assigning values later, otherwise an error will be reported.
Viola: schema means inheriting the definition of schema and referencing the constraints of schema
Viola: {... It's a real assignment.
Execute cue eval check.cue to see the data after rendering
Schema: {name: string age: int human: true} viola: {name: "Viola" age: 38 human: true}
At this point, what kind of input will there be if you modify the check.cue as follows?
Schema: {name: string age: int human: true / / always true} viola: schemaviola: {name: "Viola" age: 38 human: false}
Execute cue eval check.cue
Viola.human: conflicting values false and true:. / check.cue:4:12. / check.cue:6:8. / check.cue:10:12
You can see that an error was reported when assigning human to false.
Definitions
In CUE, patterns are usually written as definitions. A definition is a field whose identifier begins with # or _ #. This tells CUE that they are for validation and should not be output as data; they can be unspecified.
A definition also tells CUE all the allowed fields. In other words, the definition defines a "closed" structure. Include one in the structure. You can keep it open.
Suppose the schema.cue file is as follows:
# Conn: {address: string port: int protocol: string / /... / / uncomment this to allow any field} lossy: # Conn & {address: "1.2.3.4" port: 8888 protocol: "udp" / / foo: 2 / / uncomment this to get an error}
# Conn affirms a definition
Lossy refers to this definition and instantiates the data, which is checked against schema when instantiated, and an error is reported in the following cases
Too many fields
Missing field
Field types do not match
Perform a cue export schema.cue view:
{"lossy": {"address": "1.2.3.4", "port": 8888, "protocol": "udp"}} verification
Constraints can be used to validate the value of a specific instance. They can be applied to CUE data or directly to YAML or JSON.
Suppose the schema.cue content is as follows:
# Language: {tag: string name: = ~ "^\\ p {Lu}" / / Must start with an uppercase letter.} languages: [. # Language]
Languages: [. # Language] A list of variable length, which can be compared with the eval output of languages: [# Language] to see the difference
/ / languages: output of [. # Language] # Language: {tag: string name: = ~ "^\\ p {Lu}"} languages: [] / / languages: [# Language] output # Language: {tag: string name: = ~ "^\\ p {Lu}"} languages: [{tag: string name: = ~ "^\ p {Lu}"}]
The data.yaml is as follows:
If you execute cue vet schema.cue data.yaml, you can see that an error will be reported.
Languages.1.name: invalid value "dutch" (does not match = ~ "^\\ p {Lu}"):. / schema.cue:3:8. / data.yaml:5:12
Dutch validation failed and must start with an uppercase letter
Order doesn't matter.
The basic operation of CUE is defined as this: the order in which you combine the two configurations has nothing to do with the result.
This is a key attribute of CUE, which makes it easy for humans and machines to reason about numerical values, and makes advanced tools and automation possible.
Suppose the order.cue content is as follows:
A: {x: 1, y: int} a: {x: int, y: 2} b: {x: int, y: 2} b: {x: 1, y: int}
Execute cue eval order.cue to get:
A: {x: 1 y: 2} b: {x: 1 y: 2}
As you can see, it doesn't matter who defines and assigns values first.
Folding of single structure
In JSON, people define nested values, one value at a time. Alternatively, the JSON configuration is a set of path-value pairs.
In CUE, people define a set of paths and apply specific values or constraints at once. Because of the sequential independence of the CUE, the values are merged.
The fold.cue content is as follows:
/ / path-value pairsouter: middle1: inner: 3outer: middle2: inner: 7// collection-constraint pairouter: [string]: inner: int
Execute cue export fold.cue
{"outer": {"middle1": {"inner": 3}, "middle2": {"inner": 7} "what is CUE", thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.