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