structmappers

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: 2 Imported by: 0

Documentation

Overview

Package structmappers implements some useful transformations from one struct to another, in a "functional options" style for the morph.Struct.Map method.

Each transformer receives a new copy that it is free to mutate.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendFields

func AppendFields(fields []morph.Field) morph.StructMapper

AppendFields returns a new morph.StructMapper that adds the given fields to the end of a struct's list of fields.

AppendFields is reversible with Reverse.

func Compose

func Compose(mappers ...morph.StructMapper) morph.StructMapper

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

func PrependFields

func PrependFields(fields []morph.Field) func(in morph.Struct) morph.Struct

PrependFields returns a new morph.StructMapper that inserts the given fields at the start of a struct's list of fields.

PrependFields is reversible with Reverse.

func Rename

func Rename(name string) morph.StructMapper

Rename returns a new morph.StructMapper that sets the struct's name to the provided string.

In the provided name, the token "$" is rewritten to the existing name. For example, Rename("$Xml") on a struct named "Foo" maps to a struct named "FooXml".

Rename is reversible with Reverse.

Example
package main

import (
	"fmt"

	"github.com/tawesoft/morph"
	"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"))
	fmt.Println(orange)

	appleAgain := orange.Map(structmappers.Reverse)
	fmt.Println(appleAgain)

}
Output:

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

func Reverse

func Reverse(input morph.Struct) morph.Struct

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

func SetComment

func SetComment(comment string) morph.StructMapper

SetComment returns a new morph.StructMapper that sets the struct's comment to the provided string.

func StripComment

func StripComment(s morph.Struct) morph.Struct

StripComment is a morph.StructMapper that sets the struct's comment to the empty string.

Types

This section is empty.

Jump to

Keyboard shortcuts

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