Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 82 additions & 44 deletions pkg/codegen/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package codegen

import (
"errors"
"fmt"
"reflect"
)

Expand Down Expand Up @@ -29,6 +30,78 @@ type Configuration struct {
NoVCSVersionOverride *string `yaml:"-"`
}

// Validate checks whether Configuration represent a valid configuration
func (o Configuration) Validate() error {
if o.PackageName == "" {
return errors.New("package name must be specified")
}

// Only one server type should be specified at a time.
nServers := 0
if o.Generate.IrisServer {
nServers++
}
if o.Generate.ChiServer {
nServers++
}
if o.Generate.FiberServer {
nServers++
}
if o.Generate.EchoServer {
nServers++
}
if o.Generate.GorillaServer {
nServers++
}
if o.Generate.StdHTTPServer {
nServers++
}
if o.Generate.GinServer {
nServers++
}
if nServers > 1 {
return errors.New("only one server type is supported at a time")
}

var errs []error
if problems := o.Generate.Validate(); problems != nil {
for k, v := range problems {
errs = append(errs, fmt.Errorf("`generate` configuration for %v was incorrect: %v", k, v))
}
}

if problems := o.Compatibility.Validate(); problems != nil {
for k, v := range problems {
errs = append(errs, fmt.Errorf("`compatibility-options` configuration for %v was incorrect: %v", k, v))
}
}

if problems := o.OutputOptions.Validate(); problems != nil {
for k, v := range problems {
errs = append(errs, fmt.Errorf("`output-options` configuration for %v was incorrect: %v", k, v))
}
}

err := errors.Join(errs...)
if err != nil {
return fmt.Errorf("failed to validate configuration: %w", err)
}

return nil
}

// UpdateDefaults sets reasonable default values for unset fields in Configuration
func (o Configuration) UpdateDefaults() Configuration {
if reflect.ValueOf(o.Generate).IsZero() {
o.Generate = GenerateOptions{
EchoServer: true,
Models: true,
EmbeddedSpec: true,
}
}
return o
}

// GenerateOptions specifies which supported output formats to generate.
type GenerateOptions struct {
// IrisServer specifies whether to generate iris server boilerplate
Expand All @@ -55,6 +128,10 @@ type GenerateOptions struct {
EmbeddedSpec bool `yaml:"embedded-spec,omitempty"`
}

func (oo GenerateOptions) Validate() map[string]string {
return nil
}

// CompatibilityOptions specifies backward compatibility settings for the
// code generator.
type CompatibilityOptions struct {
Expand Down Expand Up @@ -105,6 +182,10 @@ type CompatibilityOptions struct {
CircularReferenceLimit int `yaml:"circular-reference-limit"`
}

func (co CompatibilityOptions) Validate() map[string]string {
return nil
}

// OutputOptions are used to modify the output code in some way.
type OutputOptions struct {
// Whether to skip go imports on the generated code
Expand Down Expand Up @@ -142,49 +223,6 @@ type OutputOptions struct {
NameNormalizer string `yaml:"name-normalizer,omitempty"`
}

// UpdateDefaults sets reasonable default values for unset fields in Configuration
func (o Configuration) UpdateDefaults() Configuration {
if reflect.ValueOf(o.Generate).IsZero() {
o.Generate = GenerateOptions{
EchoServer: true,
Models: true,
EmbeddedSpec: true,
}
}
return o
}

// Validate checks whether Configuration represent a valid configuration
func (o Configuration) Validate() error {
if o.PackageName == "" {
return errors.New("package name must be specified")
}

// Only one server type should be specified at a time.
nServers := 0
if o.Generate.IrisServer {
nServers++
}
if o.Generate.ChiServer {
nServers++
}
if o.Generate.FiberServer {
nServers++
}
if o.Generate.EchoServer {
nServers++
}
if o.Generate.GorillaServer {
nServers++
}
if o.Generate.StdHTTPServer {
nServers++
}
if o.Generate.GinServer {
nServers++
}
if nServers > 1 {
return errors.New("only one server type is supported at a time")
}
func (oo OutputOptions) Validate() map[string]string {
return nil
}