addrfmt

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2019 License: Apache-2.0 Imports: 3 Imported by: 2

README

GoDoc Go Report Card Build Status

addrfmt

This is a small library and framework for validating and rendering addresses which only relies on the standard library.

Usage

You can get the runnable code in the examples folder.

package main

import (
	"os"
	"strings"

	"github.com/LUSHDigital/addrfmt"
)

var tmpl = `{{ fmt_receipient "RECIPIENT" }}
{{ txt "ADDRESS_LINE" }}
{{ txt "ADMINISTRATIVE_AREA" }}{{ if txt "POST_CODE" }}, {{ txt "POST_CODE" }}{{ end }}`

func main() {
	lines := addrfmt.Lines([][2]string{
		{"RECIPIENT", "John Doe"},
		{"ADDRESS_LINE", "Anon Ln. 42"},
		{"POST_CODE", "666 420"},
		{"ADMINISTRATIVE_AREA", "The Internet"},
	})
	err := valid(lines)
	if err != nil {
		panic(err)
	}
	funcs := map[string]interface{}{
		"fmt_receipient": func(s string) string {
			return strings.ToUpper(lines.Text(s))
		},
	}
	lines.Render(os.Stdout, tmpl, funcs)
}

var required = []string{
	"RECIPIENT",
	"ADDRESS_LINE",
	"ADMINISTRATIVE_AREA",
}

func valid(lines addrfmt.Lines) error {
	if err := lines.Exists(required...); err != nil {
		return err
	}
	line, err := lines.Line("POST_CODE")
	if err != nil {
		return err
	}
	if strings.HasSuffix(line.Text(), "666") {
		// Do some other magic validation
	}
	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Line

type Line [2]string

Line represents an address line as a tuple of a type and text value

func (Line) Text

func (l Line) Text() string

Text returns the text value

func (Line) Type

func (l Line) Type() string

Type returns the type value

type LineMissingError

type LineMissingError struct {
	// contains filtered or unexported fields
}

LineMissingError happens when the address line was expected but wasn't in the set

func (LineMissingError) Error

func (e LineMissingError) Error() string

type Lines

type Lines [][2]string

Lines represents multiple instances of Line

func (Lines) Exists

func (ls Lines) Exists(ts ...string) error

Exists checks if all the provided address line type exists

func (Lines) Line

func (ls Lines) Line(t string) (Line, error)

Line checks if an address line type exists and returns it

func (Lines) Render

func (ls Lines) Render(wr io.Writer, t string, fm template.FuncMap) error

Render address lines to a template

func (Lines) Template added in v1.1.0

func (ls Lines) Template() *template.Template

Template returns a new empty template with the basic rendering functions

func (Lines) Text

func (ls Lines) Text(t string) (s string)

Text returns the text of a line type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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