Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP constants for string enums #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions gen-go/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (c *Client) GetBooks(ctx context.Context, i *models.GetBooksInput) ([]model
if i.State != nil {
urlVals.Add("state", *i.State)
}
urlVals.Add("state_required", i.StateRequired)
if i.Published != nil {
urlVals.Add("published", (*i.Published).String())
}
Expand Down
52 changes: 52 additions & 0 deletions gen-go/models/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ package models
// Editing this file might prove futile when you re-run the swagger generate command

import (
"encoding/json"

strfmt "github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"

"github.com/go-openapi/errors"
"github.com/go-openapi/validate"
)

/*Book book
Expand All @@ -19,6 +23,10 @@ type Book struct {
*/
Author string `json:"author,omitempty"`

/* genre
*/
Genre string `json:"genre,omitempty"`

/* id
*/
ID int64 `json:"id,omitempty"`
Expand All @@ -32,8 +40,52 @@ type Book struct {
func (m *Book) Validate(formats strfmt.Registry) error {
var res []error

if err := m.validateGenre(formats); err != nil {
// prop
res = append(res, err)
}

if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}

var bookTypeGenrePropEnum []interface{}

const (
BookGenreScifi string = "scifi"
BookGenreMystery string = "mystery"
BookGenreHorror string = "horror"
)

// prop value enum
func (m *Book) validateGenreEnum(path, location string, value string) error {
if bookTypeGenrePropEnum == nil {
var res []string
if err := json.Unmarshal([]byte(`["scifi","mystery","horror"]`), &res); err != nil {
return err
}
for _, v := range res {
bookTypeGenrePropEnum = append(bookTypeGenrePropEnum, v)
}
}
if err := validate.Enum(path, location, value, bookTypeGenrePropEnum); err != nil {
return err
}
return nil
}

func (m *Book) validateGenre(formats strfmt.Registry) error {

if swag.IsZero(m.Genre) { // not required
return nil
}

// value enum
if err := m.validateGenreEnum("genre", "body", m.Genre); err != nil {
return err
}

return nil
}
59 changes: 59 additions & 0 deletions gen-go/models/genre.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package models

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
"encoding/json"

strfmt "github.com/go-openapi/strfmt"

"github.com/go-openapi/errors"
"github.com/go-openapi/validate"
)

/*Genre genre

swagger:model Genre
*/
type Genre string

const (
GenreScifi Genre = "scifi"
GenreMystery Genre = "mystery"
GenreHorror Genre = "horror"
)

// for schema
var genreEnum []interface{}

func (m Genre) validateGenreEnum(path, location string, value Genre) error {
if genreEnum == nil {
var res []Genre
if err := json.Unmarshal([]byte(`["scifi","mystery","horror"]`), &res); err != nil {
return err
}
for _, v := range res {
genreEnum = append(genreEnum, v)
}
}
if err := validate.Enum(path, location, value, genreEnum); err != nil {
return err
}
return nil
}

// Validate validates this genre
func (m Genre) Validate(formats strfmt.Registry) error {
var res []error

// value enum
if err := m.validateGenreEnum("", "body", m); err != nil {
return err
}

if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
38 changes: 29 additions & 9 deletions gen-go/models/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"

"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/go-openapi/validate"
)

Expand All @@ -13,20 +14,36 @@ var _ = json.Marshal
var _ = strconv.FormatInt
var _ = validate.Maximum
var _ = strfmt.NewFormats
var _ = swag.String

// GetBooksInput holds the input parameters for a getBooks operation.
type GetBooksInput struct {
Authors []string
Available *bool
State *string
Published *strfmt.Date
SnakeCase *string
Completed *strfmt.DateTime
MaxPages *float64
MinPages *int32
PagesToTime *float32
Authors []string
Available *bool
State *string
StateRequired string
Published *strfmt.Date
SnakeCase *string
Completed *strfmt.DateTime
MaxPages *float64
MinPages *int32
PagesToTime *float32
}

const (
// StateRequiredFinished provides the 'finished' value for the state_required input of the GetBooks operation.
StateRequiredFinished = "finished"
// StateRequiredInprogress provides the 'inprogress' value for the state_required input of the GetBooks operation.
StateRequiredInprogress = "inprogress"
)

var (
// StateFinished provides the 'finished' value for the state input of the GetBooks operation.
StateFinished = swag.String("finished")
// StateInprogress provides the 'inprogress' value for the state input of the GetBooks operation.
StateInprogress = swag.String("inprogress")
)

// Validate returns an error if any of the GetBooksInput parameters don't satisfy the
// requirements from the swagger yml file.
func (i GetBooksInput) Validate() error {
Expand All @@ -50,6 +67,9 @@ func (i GetBooksInput) Validate() error {
return err
}
}
if err := validate.Enum("state_required", "query", i.StateRequired, []interface{}{"finished", "inprogress"}); err != nil {
return err
}
if i.Published != nil {
if err := validate.FormatOf("published", "query", "date", (*i.Published).String(), strfmt.Default); err != nil {
return err
Expand Down
13 changes: 13 additions & 0 deletions gen-go/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ func newGetBooksInput(r *http.Request) (*models.GetBooksInput, error) {
}
input.State = &stateTmp

}
stateRequiredStr := r.URL.Query().Get("state_required")
if len(stateRequiredStr) == 0 {
return nil, errors.New("Parameter must be specified")
}
if len(stateRequiredStr) != 0 {
var stateRequiredTmp string
stateRequiredTmp, err = stateRequiredStr, error(nil)
if err != nil {
return nil, err
}
input.StateRequired = stateRequiredTmp

}
publishedStr := r.URL.Query().Get("published")
if len(publishedStr) != 0 {
Expand Down
Loading