form

package module
v0.0.0-...-ca521a6 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: Apache-2.0 Imports: 6 Imported by: 44

README

Form

Build Status

Form is a package for handling HTTP web forms input. See godoc for details:

godoc github.com/gravitational/form

Documentation

Overview

Copyright 2015 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Form is a minimalist HTTP web form parser library based on functional arguments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(r *http.Request, params ...Param) error

Parse takes http.Request and form arguments that it needs to extract

import (
     "github.com/gravitational/form"
)

var duration time.Duration
var count int
name := "default" // a simple way to set default argument

err := form.Parse(r,
   form.Duration("duration", &duration),
   form.Int("count", &count, Required()), // notice the "Required" argument
   form.String("name", &name),
   )

if err != nil {
     // handle error here
}

Types

type BadParameterError

type BadParameterError struct {
	Param   string // Param is a paramter name
	Message string // Message is an error message presented to user
}

BadParameterError is returned whenever the parameter format does not match required restrictions.

func (*BadParameterError) Error

func (p *BadParameterError) Error() string

type FileWrapper

type FileWrapper struct {
	multipart.File
	// contains filtered or unexported fields
}

func (*FileWrapper) Name

func (f *FileWrapper) Name() string

Name returns file name as set during upload

type Files

type Files []*FileWrapper

Files is a slice of multipart.File that provides additional convenient method to close all files as a single operation

func (*Files) Close

func (fs *Files) Close() error

type FilesCloseError

type FilesCloseError struct {
	Errors []error
}

func (*FilesCloseError) Error

func (p *FilesCloseError) Error() string

type MissingParameterError

type MissingParameterError struct {
	Param string
}

MissingParameterError is an error that indicates that required parameter was not supplied by user.

func (*MissingParameterError) Error

func (p *MissingParameterError) Error() string

type Param

type Param func(r *http.Request) error

Param is a functional argument parameter passed to the Parse function

func Duration

func Duration(name string, out *time.Duration, predicates ...Predicate) Param

Duration extracts duration expressed as a Go duration string e.g. "1s"

func FileSlice

func FileSlice(name string, files *Files, predicates ...Predicate) Param

FileSlice reads the files uploaded with name parameter and initialized the slice of files. The files should be closed by the callee after usage, by executing f.Close() on each of them files slice will be nil if there's an error

func Int

func Int(name string, out *int, predicates ...Predicate) Param

Int extracts the integer argument in decimal format e.g. "10"

func String

func String(name string, out *string, predicates ...Predicate) Param

String extracts the argument by name as is without any changes

func StringSlice

func StringSlice(name string, out *[]string, predicates ...Predicate) Param

StringSlice extracts the string slice of arguments by name

func Time

func Time(name string, out *time.Time, predicates ...Predicate) Param

Time extracts duration expressed as in RFC 3339 format

type Predicate

type Predicate interface {
	Pass(param string, r *http.Request) error
}

Predicate provides an extensible way to check various conditions on a variable e.g. setting minimums and maximums, or parsing some regular expressions

func Required

func Required() Predicate

Required checker parameter ensures that the parameter is indeed supplied by user it returns MissingParameterError when parameter is not present

type PredicateFunc

type PredicateFunc func(param string, r *http.Request) error

PredicateFunc takes a func and converts it into a Predicate-compatible interface

func (PredicateFunc) Pass

func (p PredicateFunc) Pass(param string, r *http.Request) error

Jump to

Keyboard shortcuts

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