webformparser

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: MIT Imports: 8 Imported by: 0

README

WebFormParser

Parse HTML forms into a Go struct

With WebFromParser you can parse a HTML form that you get with a POST request into a Go struct.

Features:

  • Easy usage
  • No runtime dependencies
  • Lightweight
  • Tested with unit tests
  • OpenSource

Usage

Given you have the following HTML form:

<form>
    <input name="FieldA">
    <input name="FieldB" type="number">
    <input name="FieldC" type="checkbox">
</form>

You can parse it into a struct with:

type MyForm struct {
    FieldA string
    FieldB int
    FieldC bool
}

func handleFormPost(w http.ResponseWriter, r *http.Request) {
    form, err := webformparser.ParseForm(MyForm{}, r)
    if err != nil {
        // handle error
    }

    fmt.Printf("Value for FieldA: %s\n", form.FieldA)
}

For a complete example take a look at the Demo.

Suported types

WebFormParser currently supports the following types:

  • string
  • bool
  • int
  • int8
  • int32
  • int64
  • uint
  • uint8
  • uint32
  • uint64
  • float32
  • float64
  • time.Time
  • *time.Time
  • *multipart.File
  • multipart.FileHeader
  • *multipart.FileHeader

Tag

You can set options for a field with the webformparser tag. The different options are separated by spaces. Currently the following options are supported:

Option Description
name:<name> By default, WebFormParses uses the Name of the struct field to lookup the Field in the HTML form. If you use anther name in he HTMl form, you can set it here.
timeformat:<format> If you have a field of the type time.Time, you use this option to set the type of the field in the HTML form. Supported values are datetime, date, time and week. Default is datetime.
ignore If you set this option, the field will be ignored by WebFormParser

Documentation

Overview

With WebFromParser you can parse a HTML form that you get with a POST request into a Go struct. For more information take a look at https://codeberg.org/JakobDev/WebFromParser

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsErrParseStructTag

func IsErrParseStructTag(err error) bool

IsErrParseStructTag returns if the error is a ErrParseStructTag

func IsErrParseValue

func IsErrParseValue(err error) bool

IsErrParseValue returns if the error is a ErrParseValue

func IsErrUnsupportedType

func IsErrUnsupportedType(err error) bool

IsErrUnsupportedType returns if the error is a ErrUnsupportedType

func ParseForm

func ParseForm[T interface{}](form T, r *http.Request) (*T, error)

Types

type ErrParseStructTag

type ErrParseStructTag struct {
	TagPart string
	Text    string
}

func (ErrParseStructTag) Error

func (err ErrParseStructTag) Error() string

type ErrParseValue

type ErrParseValue struct {
	StructName string
	HtmlName   string
	Value      string
	ParseError error
}

Hallo

func (ErrParseValue) Error

func (err ErrParseValue) Error() string

type ErrUnsupportedType

type ErrUnsupportedType struct {
	FieldName string
}

func (ErrUnsupportedType) Error

func (err ErrUnsupportedType) Error() string

Jump to

Keyboard shortcuts

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