form

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 4 Imported by: 0

README

form

Package form converts form values (url.Values) into structs.

Example

Here's a quick example: we parse POST form values and then decode them into a struct:

Quickstart
package main

import (
    "fmt"
    "net/http"
    
    "codeberg.org/aur0ra/form"
)

type User struct {
    ID   uint64 `form:"id,required"`
    Name string `form:"name"`
    Age  int    `form:"age"`
}

func Handler(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    
    user, _ := form.Decode[User](r.Form)
	
    fmt.Println(user)
}
Performance

Performance can be more than doubles by reusing the decoder:

var decoder, _ := form.NewDecoder[User]()

func Handler(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
	
    user, _ := decoder.Decode(r.Form)
	
    fmt.Println(user)
}

The supported field types in the struct are:

  • string
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • bool
  • float32, float64
  • complex64, complex128
  • a pointer to one of the above types (Coming Soon)

Custom types can be decoded when they implement field.Parser.

type Parser interface {
	ParseForm(field string) (any, error)
}

Parser must return the custom type or nil.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode[T any](values url.Values) (decoded T, err error)

Decode initiates a new decoder and decodes values into struct T

func Encode

func Encode[T any](form T) url.Values

Encode T into url.Values

Types

type Decoder

type Decoder[T any] struct {
	// contains filtered or unexported fields
}

Decoder manages the fields to decode url.Values into T

func MustNewDecoder added in v0.2.0

func MustNewDecoder[T any]() *Decoder[T]

func NewDecoder

func NewDecoder[T any]() (*Decoder[T], error)

func (*Decoder[T]) Decode

func (d *Decoder[T]) Decode(values url.Values) (decoded T, err error)

type InvalidTypeErr

type InvalidTypeErr struct {
	Kind reflect.Kind
}

func (InvalidTypeErr) Error

func (i InvalidTypeErr) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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