Documentation ¶
Index ¶
- func ColumnsErrorf(format string, a ...interface{}) error
- func FormatValue(valueType, value string) (interface{}, error)
- func HasManyErrorf(format string, a ...interface{}) error
- func HasOneErrorf(format string, a ...interface{}) error
- func JsonFileErrorf(format string, a ...interface{}) error
- func NewGinEngineWithFaker(faker *ApiFaker) *gin.Engine
- func ResponseErrorMsg(err error) map[string]string
- func SeedsErrorf(format string, a ...interface{}) error
- type ApiFaker
- type Column
- func (column *Column) AddUniquenessOf(value interface{})
- func (column Column) CheckMeta() error
- func (column *Column) CheckRelationships(seedVal interface{}, model *Model) error
- func (column *Column) CheckUniquenessOf(value interface{}) bool
- func (column *Column) CheckValue(seedVal interface{}, model *Model) error
- func (column *Column) RemoveUniquenessOf(value interface{})
- type JsonType
- type LineItem
- func (li LineItem) DeleteRelatedLis(id float64, model *Model)
- func (li LineItem) Get(key string) (interface{}, bool)
- func (li *LineItem) ID() float64
- func (li LineItem) Id() interface{}
- func (li LineItem) InsertRelatedData(model *Model) LineItem
- func (li LineItem) Len() int
- func (li *LineItem) Set(key string, value interface{})
- func (li *LineItem) SetStringValue(key, value, valueType string) error
- func (li LineItem) ToMap() map[string]interface{}
- type LineItems
- type Model
- func (model *Model) Add(li LineItem) error
- func (model *Model) CheckColumnsMeta() error
- func (model *Model) CheckRelationship(seed map[string]interface{}) error
- func (model *Model) CheckRelationships() error
- func (model *Model) CheckRelationshipsMeta() error
- func (model *Model) CheckUniqueness() error
- func (model *Model) Delete(id float64)
- func (model *Model) Get(id float64) (li LineItem, ok bool)
- func (model *Model) Has(id float64) bool
- func (model *Model) Len() int
- func (model *Model) SaveToFile(path string) error
- func (model *Model) ToLineItems() LineItems
- func (model *Model) Update(id float64, li *LineItem) error
- func (model *Model) UpdateWithAttrs(id float64, ctx *gin.Context) (LineItem, error)
- func (model *Model) Validate(seed map[string]interface{}) error
- func (model *Model) ValidateSeedsValue() error
- func (model *Model) ValidateValue(seed map[string]interface{}) error
- type RestMethod
- type Route
- type Router
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ColumnsErrorf ¶
func FormatValue ¶
FormatValue format the given string value described by the given valueType
func HasManyErrorf ¶
func HasOneErrorf ¶
func JsonFileErrorf ¶
func NewGinEngineWithFaker ¶
NewGinEngineWithFaker allocate and returns a new gin.Engine pointer, added a new middleware which will check the type id param and the resource existence, if ok, set the float64 value of id named idFloat64, otherwise response 404 or 400.
func ResponseErrorMsg ¶
func SeedsErrorf ¶
Types ¶
type ApiFaker ¶
type ApiFaker struct { // Engine in charge of serving http requests *gin.Engine // ApiDir the directory contains api json files ApiDir string // Routers contains all routes use their name as the key Routers map[string]*Router // ExtMux the external mux for the real api ExtMux http.Handler // Prefix the prefix of fake apis Prefix string }
func NewWithApiDir ¶
NewWithApiDir alloactes and returns a new ApiFaker with the given dir as its ApiDir, the error will not be nil if
- dir is wrong
- json file format is wrong
- break rules described in README.md
func (*ApiFaker) CheckRelationships ¶
CheckRelationships
func (*ApiFaker) IntegrateHandler ¶
IntegrateHandler set ApiFaker's ExtMux
type Column ¶
type Column struct { Name string `json:"name"` Type string `json:"type"` Unique bool `json:"unique"` RegexpPattern string `json:"regexp_pattern"` // contains filtered or unexported fields }
func (*Column) AddUniquenessOf ¶
func (column *Column) AddUniquenessOf(value interface{})
AddValue add the give value into the Column's uniqueValues
func (Column) CheckMeta ¶
CheckType checks
- Name and Type must be present
- Type must in jsonTypes
- RegexpPattern must valid
func (*Column) CheckRelationships ¶
CheckRelationships checks the if resource exists with the xxx_id
func (*Column) CheckUniquenessOf ¶
CheckUniquenessOf checks if the given value exists
func (*Column) CheckValue ¶
CheckValue checks the value to insert database
- type
- regexp pattern matching
- uniqueness if unique is true
func (*Column) RemoveUniquenessOf ¶
func (column *Column) RemoveUniquenessOf(value interface{})
RemoveValue remove the given value from Column's uniqueValues
type JsonType ¶
type JsonType string
type LineItem ¶
type LineItem struct {
// contains filtered or unexported fields
}
func NewLineItemWithGinContext ¶
NewLineItemWithGinContext allocates and returns a new LineItem, its keys are from Model.Cloumns, values are from gin.Contex.PostForm(), error will be not nil if gin.Contex.PostForm() has no value for any key
func NewLineItemWithMap ¶
NewLineItemWithMap allocates and returns a new LineItem, using the dataMap param as the new LineItem's dataMap
func (LineItem) DeleteRelatedLis ¶
DeleteRelatedLis deletes all related data
func (LineItem) Id ¶
func (li LineItem) Id() interface{}
Id returns the value of LineItem's dataMap["id"] it will panic if got a nil or a not float64 "id"
func (LineItem) InsertRelatedData ¶
InsertRelatedData allocates and returns a new LineItem, it will has all data of the caller LineItem, it will insert all related data if the given Model's has any Column named xxx_id
func (*LineItem) SetStringValue ¶
SetStringValue formats the given string value with given valueType, sets key into LineItem formated value
type LineItems ¶
type LineItems []LineItem
type Model ¶
type Model struct { Name string `json:"resource_name"` // Seeds acts as a snapshot of the whole database Seeds []map[string]interface{} `json:"seeds"` Columns []*Column `json:"columns"` // relationships HasMany []string `json:"has_many"` HasOne []string `json:"has_one"` // Set contains runtime data Set *gset.SetThreadSafe `json:"-"` sync.RWMutex // contains filtered or unexported fields }
func NewModelWithPath ¶
NewModelWithPath allocates and returns a new Model, using the given path as it's json file path
func (*Model) CheckColumnsMeta ¶
checkColumnsMeta checks columns:
- id must be the first column, its type must be number
- CheckMeta
func (*Model) CheckRelationship ¶
CheckRelationship
- checks if every resource in HasOne and HasMany exists
- CheckRelationships
func (*Model) CheckRelationships ¶
CheckRelationships
func (*Model) CheckRelationshipsMeta ¶
------Check------// CheckRelationshipsMeta check the uniqueness of every element in HasOne and HasMany
func (*Model) CheckUniqueness ¶
CheckUniqueness check uniqueness for initialization for ApiFaker
func (*Model) SaveToFile ¶
SaveToFile save model to file with the given path
func (*Model) ToLineItems ¶
ToLineItems allocate a new LineItems filled with Model elements slice
func (*Model) UpdateWithAttrs ¶
UpdateWithAttrsInGinContext finds a LineItem with id param, updates it with attrs from gin.Contex.PostForm(), returns the edited LineItem
func (*Model) ValidateSeedsValue ¶
ValidateSeedsValue
func (*Model) ValidateValue ¶
ValidateValue checks specific seed
type RestMethod ¶
type RestMethod int
const ( GET RestMethod = 1 << (10 * iota) POST PUT PATCH DELETE )
type Route ¶
type Route struct { // Method request method only supports GET, POST, PUT, PATCH, DELETE Method RestMethod // Path Path string }
type Router ¶
func NewRouterWithPath ¶
NewRouterWithPath allocates and returns a new Router with the givn file path