marshallable

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2022 License: MIT Imports: 10 Imported by: 0

README

marshallable

Make generic data types marshallable!

Features

Implement methods:

  • String() (string)
  • MarshalJSON() ([]byte, error)
  • UnmarshalJSON([]byte) (error)

for the following types:

  • time.Duration
  • net.IP
  • url.URL
  • mail.Address
  • regexp.Regexp

and common data types:

  • TCP/UDP port number

Usage

Scenes: for configuration initialization and deserialization

package main

import (
	"encoding/json"
	"fmt"

	m "github.com/dacapoday/marshallable"
)

type Config struct {
	ServerName    string        `json:"server_name"`
	ServerSecret  []byte        `json:"server_secret"`
	Timeout       m.Duration    `json:"timeout"`
	ListenPort    m.Port        `json:"listen_port"`
	ListenAddress m.IP          `json:"listen_address"`
	ApiServer     m.URL         `json:"api_server"`
	Contact       m.MailAddress `json:"contact"`
	SpamRule      m.Regexp      `json:"spam_rule"`
}

var base Config

func init() {
	// initialize Config with constant values
	base = Config{
		ServerName:    "server-name",
		ServerSecret:  []byte("binary encoded with base64"),
		Timeout:       m.Duration{10 * time.Second},
		ListenPort:    m.Port{8080},
		ListenAddress: m.MustIP("10.0.0.1"),
		ApiServer:     m.MustURL("http://api.example.com"),
		Contact:       m.MustMailAddress("who@example.com"),
		SpamRule:      m.MustRegexp("^[a-zA-Z0-9]{4,}$"),
	}
}

func main() {
	fmt.Println("marshal:")
	data, err := json.Marshal(base)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", data)

	fmt.Println("unmarshal:")
	config := new(Config)
	err = json.Unmarshal(data, config)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", config)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidIP = errors.New("invalid IP")
View Source
var ErrInvalidIPv4 = errors.New("invalid IPv4")
View Source
var ErrInvalidIPv6 = errors.New("invalid IPv6")

Functions

This section is empty.

Types

type Duration

type Duration struct{ time.Duration }

Duration is a time.Duration wraper that marshals to and from a JSON string.

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type IP

type IP struct {
	net.IP
}

func MustIP

func MustIP(value string) IP

MustIP returns the IP or panics.

func (IP) MarshalJSON

func (ip IP) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (IP) String

func (ip IP) String() string

String returns the IP as a string.

func (*IP) UnmarshalJSON

func (ip *IP) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type IPv4

type IPv4 struct {
	net.IP
}

func MustIPv4

func MustIPv4(value string) IPv4

MustIPv4 returns the IPv4 or panics.

func (IPv4) MarshalJSON

func (ip IPv4) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (IPv4) String

func (ip IPv4) String() string

String returns the IP as a string.

func (*IPv4) UnmarshalJSON

func (ip *IPv4) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type IPv6

type IPv6 struct {
	net.IP
}

func MustIPv6

func MustIPv6(value string) IPv6

MustIPv6 returns the IPv6 or panics.

func (IPv6) MarshalJSON

func (ip IPv6) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (IPv6) String

func (ip IPv6) String() string

String returns the IP as a string.

func (*IPv6) UnmarshalJSON

func (ip *IPv6) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type MailAddress

type MailAddress struct {
	*mail.Address
}

MailAddress is a mail.Address wrapper that marshals to and from a JSON string.

func MustMailAddress

func MustMailAddress(value string) MailAddress

MustMailAddress returns the MailAddress or panics.

func (MailAddress) MarshalJSON

func (m MailAddress) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (MailAddress) String

func (m MailAddress) String() string

String returns the MailAddress as a string.

func (*MailAddress) UnmarshalJSON

func (m *MailAddress) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type POSIXRegexp

type POSIXRegexp struct{ *regexp.Regexp }

POSIXRegexp is a POSIX regexp.Regexp wraper that marshals to and from a JSON string.

func MustPOSIXRegexp

func MustPOSIXRegexp(value string) POSIXRegexp

MustPOSIXRegexp returns the POSIXRegexp or panics.

func (POSIXRegexp) MarshalJSON

func (r POSIXRegexp) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (POSIXRegexp) String

func (r POSIXRegexp) String() string

String returns the POSIXRegexp as a string.

func (*POSIXRegexp) UnmarshalJSON

func (r *POSIXRegexp) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type Port

type Port struct{ Port uint16 }

Port is a TCP/UDP port number that marshals to and from a JSON string or number.

func (Port) MarshalJSON

func (p Port) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Port) String

func (p Port) String() string

String returns the port number as a string.

func (*Port) UnmarshalJSON

func (p *Port) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type Regexp

type Regexp struct{ *regexp.Regexp }

Regexp is a regexp.Regexp wraper that marshals to and from a JSON string.

func MustRegexp

func MustRegexp(value string) Regexp

MustRegexp returns the Regexp or panics.

func (Regexp) MarshalJSON

func (r Regexp) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Regexp) String

func (r Regexp) String() string

String returns the Regexp as a string.

func (*Regexp) UnmarshalJSON

func (r *Regexp) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type URL

type URL struct{ *url.URL }

URL is a net/url.URL wraper that marshals to and from a JSON string.

func MustURL

func MustURL(value string) URL

MustURL returns the URL or panics.

func (URL) MarshalJSON

func (u URL) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (URL) String

func (u URL) String() string

String returns the URL as a string.

func (*URL) UnmarshalJSON

func (u *URL) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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