-
-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for jsonapi in models generator (#434)
* Add support for jsonapi in models generator Replaces #230 Credits to @leodotcloud for the original PR based on the old generator. * Fix String() for jsonapi models
- Loading branch information
1 parent
fa578ca
commit aa19b35
Showing
7 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package models | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
"github.com/gobuffalo/nulls" | ||
"github.com/gobuffalo/pop" | ||
"github.com/gobuffalo/validate" | ||
"github.com/gobuffalo/validate/validators" | ||
"github.com/gofrs/uuid" | ||
"github.com/google/jsonapi" | ||
) | ||
|
||
type Widget struct { | ||
ID uuid.UUID `jsonapi:"primary,id" db:"id"` | ||
CreatedAt time.Time `jsonapi:"attr,created_at" db:"created_at"` | ||
UpdatedAt time.Time `jsonapi:"attr,updated_at" db:"updated_at"` | ||
Name string `jsonapi:"attr,name" db:"name"` | ||
Description string `jsonapi:"attr,description" db:"description"` | ||
Age int `jsonapi:"attr,age" db:"age"` | ||
Bar nulls.String `jsonapi:"attr,bar" db:"bar"` | ||
} | ||
|
||
// String is not required by pop and may be deleted | ||
func (w Widget) String() string { | ||
var jb strings.Builder | ||
_ = jsonapi.MarshalPayload(&jb, &w) | ||
return jb.String() | ||
} | ||
|
||
// Widgets is not required by pop and may be deleted | ||
type Widgets []Widget | ||
|
||
// String is not required by pop and may be deleted | ||
func (w Widgets) String() string { | ||
var jb strings.Builder | ||
_ = jsonapi.MarshalPayload(&jb, &w) | ||
return jb.String() | ||
} | ||
|
||
// Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method. | ||
// This method is not required and may be deleted. | ||
func (w *Widget) Validate(tx *pop.Connection) (*validate.Errors, error) { | ||
return validate.Validate( | ||
&validators.StringIsPresent{Field: w.Name, Name: "Name"}, | ||
&validators.StringIsPresent{Field: w.Description, Name: "Description"}, | ||
&validators.IntIsPresent{Field: w.Age, Name: "Age"}, | ||
), nil | ||
} | ||
|
||
// ValidateCreate gets run every time you call "pop.ValidateAndCreate" method. | ||
// This method is not required and may be deleted. | ||
func (w *Widget) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) { | ||
return validate.NewErrors(), nil | ||
} | ||
|
||
// ValidateUpdate gets run every time you call "pop.ValidateAndUpdate" method. | ||
// This method is not required and may be deleted. | ||
func (w *Widget) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) { | ||
return validate.NewErrors(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters