...

Source file src/cuelang.org/go/cuego/doc.go

Documentation: cuelang.org/go/cuego

     1  // Copyright 2019 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 cuego allows using CUE constraints in Go programs.
    16  //
    17  // CUE constraints can be used to validate Go types as well as fill out
    18  // missing struct fields that are implied from the constraints and the values
    19  // already defined by the struct value.
    20  //
    21  // CUE constraints can be added through field tags or by associating
    22  // CUE code with a Go type. The field tags method follows the usual
    23  // Go pattern:
    24  //
    25  //	type Sum struct {
    26  //	    A int `cue:"C-B" json:",omitempty"`
    27  //	    B int `cue:"C-A" json:",omitempty"`
    28  //	    C int `cue:"A+B" json:",omitempty"`
    29  //	}
    30  //
    31  //	func main() {
    32  //	    fmt.Println(cuego.Validate(&Sum{A: 1, B: 5, C: 6}))
    33  //	}
    34  //
    35  // AddConstraints allows annotating Go types with any CUE constraints.
    36  //
    37  // # Validating Go Values
    38  //
    39  // To check whether a struct's values satisfy its constraints, call Validate:
    40  //
    41  //	if err := cuego.Validate(p); err != nil {
    42  //	   return err
    43  //	}
    44  //
    45  // Validation assumes that all values are filled in correctly and will not
    46  // infer values. To automatically infer values, use Complete.
    47  //
    48  // # Completing Go Values
    49  //
    50  // Package cuego can also be used to infer undefined values from a set of
    51  // CUE constraints, for instance to fill out fields in a struct. A value
    52  // is considered undefined if it is a nil pointer type or if it is a zero
    53  // value and there is a JSON field tag with the omitempty flag.
    54  // A Complete will implicitly validate a struct.
    55  package cuego // import "cuelang.org/go/cuego"
    56  
    57  // The first goal of this packages is to get the semantics right. After that,
    58  // there are a lot of performance gains to be made:
    59  // - cache the type info extracted during value (as opposed to type) conversion
    60  // - remove the usage of mutex for value conversions
    61  // - avoid the JSON round trip for Decode, as used in Complete
    62  // - generate native code for validating and updating
    63  

View as plain text