hasty

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2021 License: MIT Imports: 8 Imported by: 1

README

Hasty

Go Reference

Hasty is a library providing functions that are useful for quick'n'dirty coding (small "script" programs, puzzle solving, etc).

Not for use in production code.

Documentation

Overview

Package hasty provides functions that are useful for quick'n'dirty coding (small "script" programs, puzzle solving, etc).

Not for use in production code.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoMatch = errors.New("hasty: provided regular expression did not match data")

ErrNoMatch is returned by Parse if the regular expression did not match the

Functions

func MustParse

func MustParse(data []byte, v interface{}, r *regexp.Regexp)

MustParse is like Parse except that it panics instead of returning a non-nil error.

func Parse

func Parse(data []byte, v interface{}, r *regexp.Regexp) error

Parse uses r to parse data and loads it into v.

The target v must be a pointer to a value of struct type. The exported struct fields correspond to named capture groups of r. The following types are supported:

  • If the field implements encoding.TextUnmarshaler, then that is used.
  • If the field is a string or a []byte, the matching string is copied as-is.
  • If the field is any integer type, the captured string is parsed as a decimal integer.

If the struct value contains any other exported fields, Parse returns a non-nil error.

If r contains any named capture groups which don't correspond to a field in v, Parse returns a non-nil error. Unnamed capture groups are ignored.

Example
package main

import (
	"fmt"
	"log"
	"regexp"

	"github.com/cespare/hasty"
)

func main() {
	var person struct {
		Name string
		Age  int
	}
	data := `{Alice Smith, 44}`
	re := regexp.MustCompile(`^{(?P<Name>[\w ]+),\s+(?P<Age>\d+)}$`)
	if err := hasty.Parse([]byte(data), &person, re); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s (%d)\n", person.Name, person.Age)
}
Output:

Alice Smith (44)

Types

This section is empty.

Jump to

Keyboard shortcuts

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