transform

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2019 License: MIT Imports: 3 Imported by: 16

Documentation

Overview

Package transform provides mechanisms to transform a data source, with a specific focus on JSON data handling.

Copyright © Trevor N. Suarez (Rican7)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(data []byte, direction Direction, transformers ...Transformer) []byte

Bytes takes a source bytes of data, a Direction, and a variable number of transformers and returns a new byte slice with the result each transformer having run on a copy of the original passed data.

Types

type Direction

type Direction bool

Direction flags the direction of marshaling/unmarshaling (encoding/decoding).

const (
	// Marshal defines the direction of marshaling or encoding.
	Marshal Direction = false

	// Unmarshal defines the direction of unmarshaling or decoding.
	Unmarshal Direction = true
)

func (Direction) String

func (d Direction) String() string

String satisfies the fmt.Stringer interface to provide a human-readable name for a Direction value.

type Transformer

type Transformer func([]byte, Direction) []byte

Transformer defines a function that transforms a source bytes of data, in a given direction, to a result of bytes.

func AlwaysAsDirection

func AlwaysAsDirection(always Direction, transform Transformer) Transformer

AlwaysAsDirection takes a given direction and a Transformer and returns a new Transformer that always executes the given Transformer as if the transformation direction was the given direction.

func CamelCaseKeys

func CamelCaseKeys(lowerRepeatedCaps bool) Transformer

CamelCaseKeys returns a Transformer that converts every JSON "key" (or JSON object "name") in the transformed data set to be in `camelCase` style.

If the passed lowerRepeatedCaps param is `true`, then repeated capital letters (such as "URL" or "HTTP") will be converted to typical "Title" case (such as "Url" or "Http").

func ConventionalKeys

func ConventionalKeys() Transformer

ConventionalKeys returns a Transformer that converts every JSON "key" (or JSON object "name") in the transformed data set, depending on the transformation direction, based on common JSON data style conventions.

For the "Marshal" direction, JSON keys are converted to `snake_case` style. For the "Unmarshal" direction, JSON keys are converted to `camelCase` style.

func OnlyForDirection

func OnlyForDirection(only Direction, transform Transformer) Transformer

OnlyForDirection takes a given direction and a Transformer and returns a new Transformer that only executes the given Transformer when the transformation direction matches the given direction.

func ReverseDirection

func ReverseDirection(transform Transformer) Transformer

ReverseDirection takes a Transformer and returns a new Transformer that executes the given Transformer as if the transformation direction was the oppopsite of the given direction.

func ValidIdentifierKeys

func ValidIdentifierKeys() Transformer

ValidIdentifierKeys returns a Transformer that converts every JSON "key" (or JSON object "name") in the transformed data set to a key that's format matches the Go specification to be considered a valid "identifier". It does this conversion by simply stripping characters from the key/name that would otherwise make for an invalid Go identifier, according to specification.

https://golang.org/ref/spec#Identifiers

Jump to

Keyboard shortcuts

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