scanner

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package scanner helps to scan struct for collection request info eg:

type InBody struct {
	Name    string `json:"name,nocase,strictcase"`
	Data    []byte `json:"data"`
	Ignored any    `name:"-"`
	NoTag   any
	Strings []string
}
type InPath struct {
	OrgID  int `in:"path" name:"orgID"`
	UserID int `in:"path" name:"userID"`
}
type InQuery struct {
	Q0 int    `in:"query" name:"q0,format:bin"`
	Q1 string `in:"query" name:"q1"`
}
type InHeader struct {
	V1 int    `in:"header" name:"k1"`
	V2 string `in:"header" name:"k2"`
}
type InCookie struct {
	Token string `in:"cookie" name:"token"`
	CookiePayload
}
type CookiePayload struct {
	Userdata string `in:"cookie" name:"userdata"`
}
type Request struct {
	httpx.MethodGet `path:"/org/{orgID}/user/{userID}"`

	Direct bool `in:"query" name:"direct"`
	InCookie
	InHeader
	InPath
	*InQuery
	InBody  `in:"body" mime:"application/json"`
}

Request meta: 1. `in` tag describes data location. eg: path, query, header, cookie, body 2. `name` tag describes data key. 3. `path` tag describes route path. eg: GET /api/v0/user/{userID} 4. json/v2 tag extension is supported. eg: inline, unknown, string, format etc.

eg: request content GET /org/1001/user/8888?direct=true&q0=1010&q1=active HTTP/1.1 Host: api.yourservice.com k1: 200 k2: high-priority Cookie: token=abc_session_99; userdata=gold_member Content-Type: application/json

{
    "name": "xoctopus",
    "data": "SGVsbG8gV29ybGQ=",
    "strings": ["opt1", "opt2"]
}

Collect results: 1. InPath{ OrgID: 1; UserID: 2} 2. InQuery{ Q0: 101; Q1: "active"}; Direct: true 3. InHeader{ V1: 200; V2: "high-priority" } 4. InCookie{ Token: "abc_session_99" Userdata: "gold_member" } 5. InBody{ Name: "xoctopus"; Data: []byte("Hello World"); Strings: []string{"opt1","opt2"}

Index

Constants

This section is empty.

Variables

View Source
var Structs = &structs{
	fields: syncx.NewXmap[reflect.Type, *Struct](),
}

Functions

func CanUnmarshalByString

func CanUnmarshalByString(typ reflect.Type) bool

func Implements

func Implements(typ, tar reflect.Type) bool

func UnwrapField

func UnwrapField(v any) any

func WrapField

func WrapField(v any, f *Field) any

Types

type Casing

type Casing uint8
const (
	IgnoreCase Casing = 1
	StrictCase Casing = 2
)

type Field

type Field struct {
	FieldOptions

	FieldName string
	Tag       reflect.StructTag
	Type      reflect.Type
	// contains filtered or unexported fields
}

func (*Field) GetOrNewAt

func (f *Field) GetOrNewAt(v reflect.Value) reflect.Value

func (*Field) Multiple

func (f *Field) Multiple() bool

func (*Field) PatchOptions

func (f *Field) PatchOptions(o ...json.Options) (next json.Options)

PatchOptions next options inherited from o and patched by f's attributes

type FieldOptions

type FieldOptions struct {
	Name       string
	HasName    bool
	Casing     Casing
	Inline     bool
	Unknown    bool
	Omitzero   bool
	Omitempty  bool
	String     bool
	Format     string
	StringItem bool

	// Embedded not in v2.fieldOptions. for parsing duplicated query kvs
	// eg: createdAt.gt=x&createdAt.lte=y&updatedAt.gt=x&updatedAt.lte=y
	// the gt, lte in createdAt and updatedAt is same type but prefix.
	// This is a temporary plan
	Embedded bool
}

FieldOptions see encoding/json/v2.fieldOptions. nameNeedEscape is unsupported

func ParseFieldOptions

func ParseFieldOptions(sf reflect.StructField) (FieldOptions, bool, error)

func (*FieldOptions) StrictCase

func (o *FieldOptions) StrictCase() bool

type Struct

type Struct struct {
	// contains filtered or unexported fields
}

func (*Struct) FieldsIn

func (fs *Struct) FieldsIn(loc string) iter.Seq[*Field]

func (*Struct) FieldsInCookie

func (fs *Struct) FieldsInCookie() iter.Seq[*Field]

func (*Struct) FieldsInHeader

func (fs *Struct) FieldsInHeader() iter.Seq[*Field]

func (*Struct) FieldsInPath

func (fs *Struct) FieldsInPath() iter.Seq[*Field]

func (*Struct) FieldsInQuery

func (fs *Struct) FieldsInQuery() iter.Seq[*Field]

func (*Struct) Inlined

func (fs *Struct) Inlined() (*Field, bool)

func (*Struct) Len

func (fs *Struct) Len() int

func (*Struct) Lookup

func (fs *Struct) Lookup(name string) (*Field, bool)

func (*Struct) LookupIn

func (fs *Struct) LookupIn(in, name string) (*Field, bool)

func (*Struct) Range

func (fs *Struct) Range(fn func(f *Field) bool)

func (*Struct) RangeIn

func (fs *Struct) RangeIn(loc string) iter.Seq[*Field]

type WrappedField

type WrappedField interface {
	Unwrap() any
	Field() *Field
}

Jump to

Keyboard shortcuts

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