urlstruct

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2020 License: BSD-2-Clause Imports: 12 Imported by: 10

README

urlstruct decodes url.Values into structs

Build Status GoDoc

Example

Following example decodes URL query ?page=2&limit=100&author_id=123 into a struct.

type Book struct {
	tableName struct{} `pg:"alias:b"`

	ID        int64
	AuthorID  int64
	CreatedAt time.Time
}

type BookFilter struct {
	tableName struct{} `urlstruct:"b"`

	urlstruct.Pager
	AuthorID int64
}

func ExampleUnmarshal_filter() {
	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(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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal added in v0.2.1

func Unmarshal(ctx context.Context, values url.Values, strct interface{}) error

Unmarshal unmarshals the URL query values into the struct.

Types

type Field

type Field struct {
	Type  reflect.Type
	Name  string
	Index []int
	Tag   *tagparser.Tag
	// contains filtered or unexported fields
}

func (*Field) Value

func (f *Field) Value(strct reflect.Value) reflect.Value

type Pager

type Pager struct {
	Limit  int
	Offset int

	// Default is 100.
	DefaultLimit int `urlstruct:"-"`
	// Default is 1000.
	MaxLimit int `urlstruct:"-"`

	// Default max offset is 1000000.
	MaxOffset int `urlstruct:"-"`
	// contains filtered or unexported fields
}

func NewPager

func NewPager(values url.Values) *Pager

func (*Pager) GetLimit

func (p *Pager) GetLimit() int

func (*Pager) GetOffset

func (p *Pager) GetOffset() int

func (*Pager) GetPage

func (p *Pager) GetPage() int

func (*Pager) SetPage

func (p *Pager) SetPage(page int)

func (*Pager) UnmarshalValues added in v0.2.0

func (p *Pager) UnmarshalValues(ctx context.Context, values url.Values) error

type ParamUnmarshaler added in v0.4.0

type ParamUnmarshaler interface {
	UnmarshalParam(ctx context.Context, name string, values []string) error
}

type StructInfo

type StructInfo struct {
	// 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

type Unmarshaler interface {
	UnmarshalValues(ctx context.Context, values url.Values) error
}

type Values

type Values url.Values

func (Values) Bool

func (v Values) Bool(name string) (bool, error)

func (Values) Duration

func (v Values) Duration(name string) (time.Duration, error)

func (Values) Float64

func (v Values) Float64(name string) (float64, error)

func (Values) Has

func (v Values) Has(name string) bool

func (Values) Int

func (v Values) Int(name string) (int, error)

func (Values) Int64

func (v Values) Int64(name string) (int64, error)

func (Values) MaybeBool

func (v Values) MaybeBool(name string) bool

func (Values) MaybeDuration

func (v Values) MaybeDuration(name string) time.Duration

func (Values) MaybeFloat64

func (v Values) MaybeFloat64(name string) float64

func (Values) MaybeInt

func (v Values) MaybeInt(name string) int

func (Values) MaybeInt64

func (v Values) MaybeInt64(name string) int64

func (Values) MaybeTime

func (v Values) MaybeTime(name string) time.Time

func (Values) Pager

func (v Values) Pager() *Pager

func (Values) SetDefault

func (v Values) SetDefault(name string, values ...string)

func (Values) String

func (v Values) String(name string) string

func (Values) Strings

func (v Values) Strings(name string) []string

func (Values) Time

func (v Values) Time(name string) (time.Time, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL