All Projects → monoculum → Formam

monoculum / Formam

Licence: apache-2.0
a package for decode form's values into struct in Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Formam

Form
🚂 Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. Dual Array and Full map support.
Stars: ✭ 454 (+183.75%)
Mutual labels:  decoding, form
Android Quick Response Code
Android QR Code Decoder and Encoder
Stars: ✭ 158 (-1.25%)
Mutual labels:  decoding
Avue
Avue.js2.0是基于现有的element-ui库进行的二次封装,简化一些繁琐的操作,核心理念为数据驱动视图,主要的组件库针对table表格和form表单场景,同时衍生出更多企业常用的组件,达到高复用,容易维护和扩展的框架,同时内置了丰富了数据展示组件,让开发变得更加容易
Stars: ✭ 1,789 (+1018.13%)
Mutual labels:  form
React Native Form
A simple react-native component to wrap your form fields and get their values with just one single method.
Stars: ✭ 146 (-8.75%)
Mutual labels:  form
Fit
A Go package for decoding and encoding Garmin FIT files
Stars: ✭ 128 (-20%)
Mutual labels:  decoding
Formhelper
ASP.NET Core - Transform server-side validations to client-side without writing any javascript code. (Compatible with Fluent Validation)
Stars: ✭ 155 (-3.12%)
Mutual labels:  form
Lerc
Limited Error Raster Compression
Stars: ✭ 126 (-21.25%)
Mutual labels:  decoding
Fielder
A field-first form library for React and React Native
Stars: ✭ 160 (+0%)
Mutual labels:  form
Core
The Form Tools Core.
Stars: ✭ 156 (-2.5%)
Mutual labels:  form
Convform
A jQuery plugin that transforms a form into an interactive chat.
Stars: ✭ 141 (-11.87%)
Mutual labels:  form
Crypto Rnn
Learning the Enigma with Recurrent Neural Networks
Stars: ✭ 139 (-13.12%)
Mutual labels:  decoding
Reactive forms
This is a model-driven approach to handling form inputs and validations, heavily inspired in Angular's Reactive Forms
Stars: ✭ 135 (-15.62%)
Mutual labels:  form
Rawloader
rust library to extract the raw data and some metadata from digital camera images
Stars: ✭ 153 (-4.37%)
Mutual labels:  decoding
Forms
📝 Simple form & survey app for Nextcloud
Stars: ✭ 127 (-20.62%)
Mutual labels:  form
Vuetify Form Base
Schema-based Form Generator - Vue.js 2.0 Component based on Vuetify 2.0
Stars: ✭ 157 (-1.87%)
Mutual labels:  form
Form Validation.js
The most customizable validation framework for JavaScript.
Stars: ✭ 127 (-20.62%)
Mutual labels:  form
Antd Schema Form
Based on Ant Design, interactive forms can be generated through JSON Schema configuration. - 基于Ant Design,可以通过JSON Schema配置生成可交互的表单。
Stars: ✭ 137 (-14.37%)
Mutual labels:  form
Ffmpeg Video Player
An FFmpeg and SDL Tutorial.
Stars: ✭ 149 (-6.87%)
Mutual labels:  decoding
Formik Effect
Declarative component for managing side-effects in Formik forms. 580 bytes
Stars: ✭ 160 (+0%)
Mutual labels:  form
Swform
iOS 高度封装自适应表单(重构版)
Stars: ✭ 159 (-0.62%)
Mutual labels:  form

formam

A Go package to decode HTTP form and query parameters. The only requirement is Go 1.10 or later.

Build Status GoDoc

Features

  • Infinite nesting for maps, structs and slices.
  • Support UnmarshalText() interface in values and keys of maps.
  • Supported map keys are string, int and variants, uint and variants, uintptr, float32, float64, bool, struct, custom types to one of the above types registered by function or UnmarshalText method, a pointer to one of the above types
  • A field with interface{} that has a map, struct or slice as value is accessible.
  • Decode time.Time with format 2006-01-02 by its UnmarshalText() method.
  • Decode url.URL.
  • Append to slice and array types without explicitly indicating an index.
  • Register a function for a custom type.

Performance

You can see the performance in formam-benchmark compared with ajg/form, gorilla/schema, go-playground/form and built-in/json.

Basic usage example

In form HTML

  • Use . to access a struct field (e.g. struct.field1).
  • Use [<index>] to access tje specific slice/array index (e.g. struct.array[0]). It's not necessary to add an index to append data.
  • Use [<key>] to access map keys (e.g.. struct.map[es-ES]).
<form method="POST">
  <input type="text" name="Name" value="Sony">
  <input type="text" name="Location.Country" value="Japan">
  <input type="text" name="Location.City" value="Tokyo">
  <input type="text" name="Products[0].Name" value="Playstation 4">
  <input type="text" name="Products[0].Type" value="Video games">
  <input type="text" name="Products[1].Name" value="TV Bravia 32">
  <input type="text" name="Products[1].Type" value="TVs">
  <input type="text" name="Founders[0]" value="Masaru Ibuka">
  <input type="text" name="Founders[0]" value="Akio Morita">
  <input type="text" name="Employees" value="90000">
  <input type="text" name="public" value="true">
  <input type="url" name="website" value="http://www.sony.net">
  <input type="date" name="foundation" value="1946-05-07">
  <input type="text" name="Interface.ID" value="12">
  <input type="text" name="Interface.Name" value="Go Programming Language">
  <input type="submit">
</form>

In Go

You can use the formam struct tag to ensure the form values are unmarshalled in the currect struct fields.

type InterfaceStruct struct {
    ID   int
    Name string
}

type Company struct {
  Public     bool      `formam:"public"`
  Website    url.URL   `formam:"website"`
  Foundation time.Time `formam:"foundation"`
  Name       string
  Location   struct {
    Country  string
    City     string
  }
  Products   []struct {
    Name string
    Type string
  }
  Founders   []string
  Employees  int64

  Interface interface{}
}

func MyHandler(w http.ResponseWriter, r *http.Request) error {
  r.ParseForm()

  m := Company{
      // it's is possible to access to the fields although it's an interface field!
      Interface: &InterfaceStruct{},
  }
  dec := formam.NewDecoder(&formam.DecoderOptions{TagName: "formam"})
  return dec.Decode(r.Form, &m)
}

Types

Supported types in the destination struct are:

  • string
  • bool
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • slice, array
  • struct and struct anonymous
  • map
  • interface{}
  • time.Time
  • url.URL
  • custom types to one of the above types
  • a pointer to one of the above types

Custom Marshaling

You can umarshal data and map keys by implementing the encoding.TextUnmarshaler interface.

If the forms sends multiple values then only the first value is passed to UnmarshalText(), but if the name ends with [] then it's called for all values.

Custom Type

You can register a function for a custom type using the RegisterCustomType() method. This will work for any number of given fields or all fields with the given type.

Registered type have preference over the UnmarshalText method unless the PrefUnmarshalText option is used.

All fields

decoder.RegisterCustomType(func(vals []string) (interface{}, error) {
        return time.Parse("2006-01-02", vals[0])
}, []interface{}{time.Time{}}, nil)

Specific fields

package main

type Times struct {
    Timestamp   time.Time
    Time        time.Time
    TimeDefault time.Time
}

func main() {
    var t Timestamp

    dec := NewDecoder(nil)

    // for Timestamp field
    dec.RegisterCustomType(func(vals []string) (interface{}, error) {
            return time.Parse("2006-01-02T15:04:05Z07:00", vals[0])
    }, []interface{}{time.Time{}}, []interface{}{&t.Timestamp{}}) 

    // for Time field
    dec.RegisterCustomType(func(vals []string) (interface{}, error) {
                return time.Parse("Mon, 02 Jan 2006 15:04:05 MST", vals[0])
    }, []interface{}{time.Time{}}, []interface{}{&t.Time{}}) 

    // for field that not be Time or Timestamp, e.g. in this example, TimeDefault.
    dec.RegisterCustomType(func(vals []string) (interface{}, error) {
                return time.Parse("2006-01-02", vals[0])
    }, []interface{}{time.Time{}}, nil)

    dec.Decode(url.Values{}, &t)
}

Notes

Version 2 is compatible with old syntax to access to maps (map.key), but brackets are the preferred way to access a map (map[key]).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].