// Copyright 2019 CUE Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package gocode import ( "bytes" "fmt" "go/ast" "go/format" "go/types" "text/template" "golang.org/x/tools/go/packages" "cuelang.org/go/cue" "cuelang.org/go/cue/errors" "cuelang.org/go/internal/value" ) // Config defines options for generation Go code. type Config struct { // Prefix is used as a prefix to all generated variables. It defaults to // cuegen. Prefix string // ValidateName defines the default name for validation methods or prefix // for validation functions. The default is "Validate". Set to "-" to // disable generating validators. ValidateName string // CompleteName defines the default name for complete methods or prefix // for complete functions. The default is "-" (disabled). CompleteName string // The cue.Runtime variable name to use for initializing Codecs. // A new Runtime is created by default. RuntimeVar string } const defaultPrefix = "cuegen" // Generate generates Go code for the given instance in the directory of the // given package. // // Generate converts top-level declarations to corresponding Go code. By default, // it will only generate validation functions of methods for exported top-level // declarations. The behavior can be altered with the @go attribute. // // The go attribute has the following form @go({,