github.com/reggieriser/pop@v4.13.1+incompatible/genny/model/_fixtures/models/widget_jsonapi.go (about)

     1  package models
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/gobuffalo/nulls"
     8  	"github.com/gobuffalo/pop"
     9  	"github.com/gobuffalo/validate"
    10  	"github.com/gobuffalo/validate/validators"
    11  	"github.com/gofrs/uuid"
    12  	"github.com/google/jsonapi"
    13  )
    14  
    15  // Widget is used by pop to map your widgets database table to your go code.
    16  type Widget struct {
    17  	ID          uuid.UUID    `jsonapi:"primary,id" db:"id"`
    18  	CreatedAt   time.Time    `jsonapi:"attr,created_at" db:"created_at"`
    19  	UpdatedAt   time.Time    `jsonapi:"attr,updated_at" db:"updated_at"`
    20  	Name        string       `jsonapi:"attr,name" db:"name"`
    21  	Description string       `jsonapi:"attr,description" db:"description"`
    22  	Age         int          `jsonapi:"attr,age" db:"age"`
    23  	Bar         nulls.String `jsonapi:"attr,bar" db:"bar"`
    24  }
    25  
    26  // String is not required by pop and may be deleted
    27  func (w Widget) String() string {
    28  	var jb strings.Builder
    29  	_ = jsonapi.MarshalPayload(&jb, &w)
    30  	return jb.String()
    31  }
    32  
    33  // Widgets is not required by pop and may be deleted
    34  type Widgets []Widget
    35  
    36  // String is not required by pop and may be deleted
    37  func (w Widgets) String() string {
    38  	var jb strings.Builder
    39  	_ = jsonapi.MarshalPayload(&jb, &w)
    40  	return jb.String()
    41  }
    42  
    43  // Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method.
    44  // This method is not required and may be deleted.
    45  func (w *Widget) Validate(tx *pop.Connection) (*validate.Errors, error) {
    46  	return validate.Validate(
    47  		&validators.StringIsPresent{Field: w.Name, Name: "Name"},
    48  		&validators.StringIsPresent{Field: w.Description, Name: "Description"},
    49  		&validators.IntIsPresent{Field: w.Age, Name: "Age"},
    50  	), nil
    51  }
    52  
    53  // ValidateCreate gets run every time you call "pop.ValidateAndCreate" method.
    54  // This method is not required and may be deleted.
    55  func (w *Widget) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) {
    56  	return validate.NewErrors(), nil
    57  }
    58  
    59  // ValidateUpdate gets run every time you call "pop.ValidateAndUpdate" method.
    60  // This method is not required and may be deleted.
    61  func (w *Widget) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
    62  	return validate.NewErrors(), nil
    63  }