github.com/solo-io/cue@v0.4.7/encoding/gocode/templates.go (about)

     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 gocode
    16  
    17  import "text/template"
    18  
    19  // Inputs:
    20  // .pkgName  the Go package name
    21  var headerCode = template.Must(template.New("header").Parse(
    22  	`// Code generated by gocode.Generate; DO NOT EDIT.
    23  
    24  package {{.pkgName}}
    25  
    26  import (
    27  	"fmt"
    28  
    29  	"github.com/solo-io/cue/cue"
    30  	"github.com/solo-io/cue/encoding/gocode/gocodec"
    31  	_ "github.com/solo-io/cue/pkg"
    32  )
    33  
    34  `))
    35  
    36  // Inputs:
    37  // .prefix 	  prefix to all generated variable names
    38  // .cueName   name of the top-level CUE value
    39  // .goType    Go type of the receiver or argument
    40  // .zero      zero value of the Go type; nil indicates no value
    41  // .validate  name of the validate function; "" means no validate
    42  // .complete  name of the complete function; "" means no complete
    43  var stubCode = template.Must(template.New("type").Parse(`
    44  var {{.prefix}}val{{.cueName}} = {{.prefix}}Make("{{.cueName}}", {{.zero}})
    45  
    46  {{ $sig := .goType | printf "(x %s)" -}}
    47  {{if .validate}}
    48  // {{.validate}}{{if .func}}{{.cueName}}{{end}} validates x.
    49  func {{if .func}}{{.validate}}{{.cueName}}{{$sig}}
    50       {{- else -}}{{$sig}} {{.validate}}(){{end}} error {
    51  	return {{.prefix}}Codec.Validate({{.prefix}}val{{.cueName}}, x)
    52  }
    53  {{end}}
    54  {{if .complete}}
    55  // {{.complete}}{{if .func}}{{.cueName}}{{end}} completes x.
    56  func {{if .func}}{{.complete}}{{.cueName}}{{$sig}}
    57       {{- else -}}{{$sig}} {{.complete}}(){{end}} error {
    58  	return {{.prefix}}Codec.Complete({{.prefix}}val{{.cueName}}, x)
    59  }
    60  {{end}}
    61  `))
    62  
    63  // Inputs:
    64  // .prefix 	  prefix to all generated variable names
    65  // .runtime   the variable name of a user-supplied runtime, if any
    66  // .data      bytes obtained from Instance.MarshalBinary
    67  var loadCode = template.Must(template.New("load").Parse(`
    68  var {{.prefix}}Codec, {{.prefix}}Instance = func() (*gocodec.Codec, *cue.Instance) {
    69  	var r *cue.Runtime
    70  	r = {{if .runtime}}{{.runtime}}{{else}}&cue.Runtime{}{{end}}
    71  	instances, err := r.Unmarshal({{.prefix}}InstanceData)
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  	if len(instances) != 1 {
    76  		panic("expected encoding of exactly one instance")
    77  	}
    78  	return gocodec.New(r, nil), instances[0]
    79  }()
    80  
    81  // {{.prefix}}Make is called in the init phase to initialize CUE values for
    82  // validation functions.
    83  func {{.prefix}}Make(name string, x interface{}) cue.Value {
    84  	f, err := {{.prefix}}Instance.Value().FieldByName(name, true)
    85  	if err != nil {
    86  		panic(fmt.Errorf("could not find type %q in instance", name))
    87  	}
    88  	v := f.Value
    89  	if x != nil {
    90  		w, err := {{.prefix}}Codec.ExtractType(x)
    91  		if err != nil {
    92  			panic(err)
    93  		}
    94  		v = v.Unify(w)
    95  	}
    96  	return v
    97  }
    98  
    99  // Data size: {{len .data}} bytes.
   100  var {{.prefix}}InstanceData = []byte({{printf "%+q" .data}})
   101  `))