fieldmappers

package
v0.0.0-...-b05fda7 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package fieldmappers provides helpful composable functions that implement morph.FieldMapper for mapping the fields between two structs using morph.

A subpackage, morph/fieldmappers/fieldops, provides additional field mappers that set the Comparer, Copier, and Orderer expressions on struct fields.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(input morph.Field, emit func(output morph.Field))

All is a morph.FieldMapper that emits every input unchanged.

func Compose

func Compose(mappers ...morph.FieldMapper) morph.FieldMapper

Compose returns a new morph.FieldMapper that applies each of the given non-nil mappers, from left to right. Nil mappers are skipped.

func Conditionally

func Conditionally(filter func(morph.Field) bool, mapper morph.FieldMapper) morph.FieldMapper

Conditionally returns a new morph.FieldMapper that applies mapper to any field where the filter func returns true, or emits the field unchanged if the filter func returns false.

func DeleteNamed

func DeleteNamed(names ...string) morph.FieldMapper

DeleteNamed returns a new morph.FieldMapper that removes the named fields from a struct.

func Filter

func Filter(filter func(input morph.Field) bool) morph.FieldMapper

Filter returns a new morph.FieldMapper that only emits fields where the provided filter function returns true.

func FilterInv

func FilterInv(filter func(input morph.Field) bool) func(input morph.Field) bool

FilterInv returns a filter that implements the inverse of the provided filter. Wherever the input filter would return true, the output filter instead returns false, and vice versa.

func FilterNamed

func FilterNamed(names ...string) func(morph.Field) bool

FilterNamed returns a filter that returns true for any field with a name matching any provided name argument.

func FilterSlices

func FilterSlices(input morph.Field) bool

FilterSlices is a filter that returns true for any field with a type that is a slice.

func FilterTypes

func FilterTypes(types ...string) func(morph.Field) bool

FilterTypes returns a filter that returns true for any field with a type name matching any provided type name argument.

func None

func None(input morph.Field, emit func(output morph.Field))

None is a morph.FieldMapper that deletes every input.

func Reverse

func Reverse(input morph.Field, emit func(output morph.Field))

Reverse is a morph.FieldMapper that maps a mapped struct back to its original, to the extent that this is possible, by applying the reverse FieldMapper on each field.

func StripComments

func StripComments(input morph.Field, emit func(output morph.Field))

StripComments is a morph.FieldMapper that strips all comments from each input field.

func StripTags

func StripTags(input morph.Field, emit func(output morph.Field))

StripTags is a morph.FieldMapper that strips all struct tags from each input field.

func TimeToInt64

func TimeToInt64(input morph.Field, emit func(output morph.Field))

TimeToInt64 is a morph.FieldMapper that converts any `time.Time` field to an `int64` field containing the time in seconds since the Unix epoch.

As it is difficult to distinguish between an int64 that's just an integer, and an int64 that used to be a time, this sets a Reverse method on output field. This allows Reverse to automatically perform the reverse mapping.

The function sets appropriate Comparer, Copier, and Orderer expressions on the output field and on the reverse output field.

Example
package main

import (
	"fmt"

	"github.com/tawesoft/morph"
	"github.com/tawesoft/morph/fieldmappers"
	"github.com/tawesoft/morph/structmappers"
)

func must[X any](value X, err error) X {
	if err != nil {
		panic(err)
	}
	return value
}

func main() {
	source := `
package example

type Apple struct {
    Picked    time.Time
    LastEaten time.Time
    Weight    weight.Weight
    Price     price.Price
}
`

	apple := must(morph.ParseStruct("test.go", source, "Apple"))
	orange := apple.Map(
		structmappers.Rename("Orange"),
	).MapFields(
		fieldmappers.TimeToInt64,
	)
	fmt.Println(orange)

}
Output:

type Orange struct {
	Picked    int64 // time in seconds since Unix epoch
	LastEaten int64 // time in seconds since Unix epoch
	Weight    weight.Weight
	Price     price.Price
}

Types

This section is empty.

Directories

Path Synopsis
Package fieldops implements morph FieldMappers that set appropriate Comparer, Copier, and Orderer expressions on morph Fields.
Package fieldops implements morph FieldMappers that set appropriate Comparer, Copier, and Orderer expressions on morph Fields.

Jump to

Keyboard shortcuts

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