Marcel van Lohuizen | 240a995 | 2019-08-05 10:29:13 +0200 | [diff] [blame] | 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( |
Marcel van Lohuizen | 0308a53 | 2020-02-12 19:19:14 +0100 | [diff] [blame] | 22 | `// Code generated by gocode.Generate; DO NOT EDIT. |
Marcel van Lohuizen | 240a995 | 2019-08-05 10:29:13 +0200 | [diff] [blame] | 23 | |
| 24 | package {{.pkgName}} |
| 25 | |
| 26 | import ( |
| 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 |
| 42 | var stubCode = template.Must(template.New("type").Parse(` |
| 43 | var {{.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. |
| 48 | func {{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. |
| 55 | func {{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 |
| 66 | var loadCode = template.Must(template.New("load").Parse(` |
| 67 | var {{.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. |
| 82 | func {{.prefix}}Make(name string, x interface{}) cue.Value { |
Marcel van Lohuizen | 0308a53 | 2020-02-12 19:19:14 +0100 | [diff] [blame] | 83 | f, err := {{.prefix}}Instance.LookupField(name) |
| 84 | if err != nil { |
Marcel van Lohuizen | 240a995 | 2019-08-05 10:29:13 +0200 | [diff] [blame] | 85 | panic(fmt.Errorf("could not find type %q in instance", name)) |
| 86 | } |
Marcel van Lohuizen | 0308a53 | 2020-02-12 19:19:14 +0100 | [diff] [blame] | 87 | v := f.Value |
Marcel van Lohuizen | 240a995 | 2019-08-05 10:29:13 +0200 | [diff] [blame] | 88 | 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. |
| 99 | var {{.prefix}}InstanceData = []byte({{printf "%+q" .data}}) |
| 100 | `)) |