Documentation
¶
Index ¶
- func Unmarshal(ctx context.Context, values url.Values, strct interface{}) error
- type Field
- type OpCode
- type Pager
- type StructInfo
- type Unmarshaler
- type Values
- func (v Values) Bool(name string) (bool, error)
- func (v Values) Duration(name string) (time.Duration, error)
- func (v Values) Float64(name string) (float64, error)
- func (v Values) Has(name string) bool
- func (v Values) Int(name string) (int, error)
- func (v Values) Int64(name string) (int64, error)
- func (v Values) MaybeBool(name string) bool
- func (v Values) MaybeDuration(name string) time.Duration
- func (v Values) MaybeFloat64(name string) float64
- func (v Values) MaybeInt(name string) int
- func (v Values) MaybeInt64(name string) int64
- func (v Values) MaybeTime(name string) time.Time
- func (v Values) Pager() *Pager
- func (v Values) SetDefault(name string, values ...string)
- func (v Values) String(name string) string
- func (v Values) Strings(name string) []string
- func (v Values) Time(name string) (time.Time, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Unmarshal ¶ added in v0.2.1
Unmarshal unmarshals url values into the struct.
Example (Filter) ¶
package main
import (
"context"
"fmt"
"net/url"
"time"
"github.com/go-pg/pg/v9"
"github.com/go-pg/urlstruct"
)
type Book struct {
tableName struct{} `pg:"alias:b"` //nolint:unused,structcheck
ID int64
AuthorID int64
CreatedAt time.Time
}
type BookFilter struct {
tableName struct{} `urlstruct:"b"` //nolint:unused,structcheck
urlstruct.Pager
AuthorID int64
}
func main() {
db := pg.Connect(&pg.Options{
User: "postgres",
Password: "",
Database: "postgres",
})
defer db.Close()
values := url.Values{
"author_id": {"123"},
"page": {"2"},
"limit": {"100"},
}
filter := new(BookFilter)
err := urlstruct.Unmarshal(context.TODO(), values, filter)
if err != nil {
panic(err)
}
filter.Pager.MaxLimit = 100 // default max limit is 1000
filter.Pager.MaxOffset = 100000 // default max offset is 1000000
// Following query generates:
//
// SELECT "b"."id", "b"."author_id", "b"."created_at"
// FROM "books" AS "b"
// WHERE "b".author_id = 123
// LIMIT 100 OFFSET 100
var books []*Book
_ = db.Model(&books).
WhereStruct(filter).
Limit(filter.Pager.GetLimit()).
Offset(filter.Pager.GetOffset()).
Select()
fmt.Println("author", filter.AuthorID)
fmt.Println("limit", filter.GetLimit())
fmt.Println("offset", filter.GetLimit())
}
Output: author 123 limit 100 offset 100
Types ¶
type Field ¶
type Pager ¶
type StructInfo ¶
type StructInfo struct {
TableName string
Fields []*Field
// contains filtered or unexported fields
}
func DescribeStruct ¶
func DescribeStruct(typ reflect.Type) *StructInfo
func (*StructInfo) Field ¶
func (s *StructInfo) Field(name string) *Field
type Unmarshaler ¶ added in v0.2.0
Click to show internal directories.
Click to hide internal directories.