toolkit

package module
v2.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2025 License: MIT Imports: 19 Imported by: 2

README

Toolkit

The included tools are:

  • Read JSON
  • Write JSON
  • Error JSON takes an error and optionally a status code, and sends a JSON error message
  • Handle Error wraps ErrorJSON and writes to the logger on failure
  • Upload a file to a specified directory
  • Download a static file
  • Get a random string of length n
  • Post JSON to a remote service
  • Write XML
  • Create a directory, including all parent directories, if it does not already exist
  • Create a URL safe slug from a string
  • Validation utilities
  • Encrypt and Decrypt capability
  • DateOnly struct (type) with JSON and SQL value & scan support
  • TimeOnly struct (type) with JSON and SQL value & scan support

Installation

go get -u github.com/fouched/toolkit/v2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encryption added in v2.4.0

type Encryption struct {
	Key []byte
}

func (*Encryption) Decrypt added in v2.4.0

func (e *Encryption) Decrypt(text string) (string, error)

func (*Encryption) Encrypt added in v2.4.0

func (e *Encryption) Encrypt(text string) (string, error)

type Field added in v2.1.0

type Field struct {
	Name  string
	Label string
	Value string
}

type JSONResponse

type JSONResponse struct {
	Error   bool        `json:"error"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

JSONResponse is the type used for sending JSON around

type Tools

type Tools struct {
	MaxFileSize        int64
	AllowedFileTypes   []string
	MaxJSONSize        int
	AllowUnknownFields bool
}

Tools is the type used to instantiate this module Any variable of this type will have access to all methods with the receiver *Tools Also see https://developer.mozilla.org/en-US/docs/Web/HTTP/MIME_types/Common_types

func (*Tools) CreateDirIfNotExist

func (t *Tools) CreateDirIfNotExist(path string) error

CreateDirIfNotExist creates a directory, and all necessary parents

func (*Tools) DownloadStaticFile

func (t *Tools) DownloadStaticFile(w http.ResponseWriter, r *http.Request, pathName, displayName string)

DownloadStaticFile downloads a file tries to force the browser to avoid displaying it in the browser window by setting content disposition. It also allows specification of the display name

func (*Tools) ErrorJSON

func (t *Tools) ErrorJSON(w http.ResponseWriter, err error, status ...int) error

ErrorJSON takes an error and optionally a status code, and sends a JSON error message

func (*Tools) HandleError added in v2.6.0

func (t *Tools) HandleError(w http.ResponseWriter, err error, logger *log.Logger) bool

HandleError wraps ErrorJSON - outputs the error JSON or writes to the logger on failure

func (*Tools) PushJSONToRemote

func (t *Tools) PushJSONToRemote(uri string, data interface{}, client ...*http.Client) (*http.Response, int, error)

PushJSONToRemote posts arbitrary data to some URL as JSON, and returns the response, status code, and error, if any The final parameter, client, is optional. If none is specified, we use the standard http.Client

func (*Tools) RandomString

func (t *Tools) RandomString(n int) string

RandomString returns a string of random characters of length n

func (*Tools) ReadJSON

func (t *Tools) ReadJSON(w http.ResponseWriter, r *http.Request, data interface{}) error

ReadJSON tries to read the body of a request and converts from json into a go data variable

func (*Tools) Slugify

func (t *Tools) Slugify(s string) (string, error)

Slugify a very simple means of creating a slug from a string

func (*Tools) UploadFiles

func (t *Tools) UploadFiles(r *http.Request, uploadDir string, rename ...bool) ([]*UploadedFile, error)

UploadFiles uploads one of more file to a specified directory, and give the files a random name if required. If the optional last parameter is set to false, it will not rename and use the original name.

func (*Tools) UploadOneFile

func (t *Tools) UploadOneFile(r *http.Request, uploadDir string, rename ...bool) (*UploadedFile, error)

UploadOneFile is a convenience methods that calls UploadFiles, but expects only one file

func (*Tools) Validator added in v2.1.0

func (t *Tools) Validator(data url.Values) *Validation

Validator creates an instance of a Validation based on form values. You can pass nil to this constructor to use the validation functions without an Http form

func (*Tools) WriteJSON

func (t *Tools) WriteJSON(w http.ResponseWriter, status int, data interface{}, headers ...http.Header) error

WriteJSON takes a response status code and arbitrary data and writes json to the client

func (*Tools) WriteXML added in v2.3.2

func (t *Tools) WriteXML(w http.ResponseWriter, status int, data interface{}, headers ...http.Header) error

WriteXML takes a response status code and arbitrary data and writes xml to the client

type UploadedFile

type UploadedFile struct {
	Key              string
	NewFileName      string
	OriginalFileName string
	FileSize         int64
}

UploadedFile is a struct used to save information about an uploaded file

type Validation added in v2.1.0

type Validation struct {
	Data   url.Values
	Errors map[string]string
}

func (*Validation) AddError added in v2.1.0

func (v *Validation) AddError(key, message string)

AddError adds an error to the current Validator instance

func (*Validation) Check added in v2.1.0

func (v *Validation) Check(ok bool, key, message string)

Check takes any expression that can be evaluated to a bool and adds an error if result is false

func (*Validation) Has added in v2.1.0

func (v *Validation) Has(field string, r *http.Request) bool

Has checks if an Http form contains a field

func (*Validation) IsDateISO added in v2.1.0

func (v *Validation) IsDateISO(field Field)

IsDateISO checks if a form field is an ISO date (YYYY-MM-DD) and adds an error if result is false

func (*Validation) IsEmail added in v2.1.0

func (v *Validation) IsEmail(field Field)

IsEmail checks if a Field contains a valid email and adds an error if result is false

func (*Validation) IsFloat added in v2.1.0

func (v *Validation) IsFloat(field Field)

IsFloat checks if a Field is a floating point number and adds an error if result is false

func (*Validation) IsInt added in v2.1.0

func (v *Validation) IsInt(field Field)

IsInt checks if a Field is an integer

func (*Validation) IsLength added in v2.1.0

func (v *Validation) IsLength(field Field, length int)

IsLength checks if a field is at least a specific length and adds an error if result is false

func (*Validation) NoSpaces added in v2.1.0

func (v *Validation) NoSpaces(field Field)

NoSpaces checks if a Field contains spaces and adds an error if result is false

func (*Validation) Required added in v2.1.0

func (v *Validation) Required(fields ...Field)

Required checks a variadic list of Field and adds an error if a field is blank

func (*Validation) Valid added in v2.1.0

func (v *Validation) Valid() bool

Valid returns true if the current Validator contains no errors, otherwise false

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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