restruct

package module
v0.0.0-...-577480b Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 3 Imported by: 0

README

reStruct

This is a small library that uses generics and reflection to provide a nice way to convert structs and types in a highly configurable way.

Example

// First format

type Foo struct {
    First  int
    Second string
}

// Second format

type SlugString string

func NewSlugString(s string) SlugString {
    return SlugString(strings.ReplaceAll(strings.TrimSpace(s), " ", "-"))
}

type Bar struct {
    FirstField  int
    SecondField SlugString
}

Then we can convert from Foo to Bar using the following

func main() {
    foo1 := Foo{
        First:  1,
        Second: "  this is my foo    ",
    }

    bar, _ := restruct.Convert[Bar](foo1,
        restruct.MustFunc[SlugString, string](NewSlugString),
        restruct.StructFromStruct[Bar, Foo]{
            "FirstField":  "First",
            "SecondField": "Second",
        },
    )

    fmt.Printf("%#v", bar)
    // Bar{
    //     FirstField:  1,
    //     SecondField: SlugString("this-is-my-foo"),
    // }
}

Usage

To install do

$ go get github.com/aziis98/go-restruct

and then import using

import "github.com/aziis98/go-restruct"

Reference

See the docs for now

TODO

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert[T any](v any, options ...Option) (T, error)

func ConvertWith

func ConvertWith[T any](cnv Converter, v any) (T, error)

Types

type Converter

type Converter struct {
	// contains filtered or unexported fields
}

func (Converter) Convert

func (c Converter) Convert(source reflect.Value, targetType reflect.Type) (v reflect.Value, err error)

type Func

type Func[Target, Source any] func(Source) (Target, error)

func (Func[Target, Source]) Convert

func (nc Func[Target, Source]) Convert(cnv Converter, source reflect.Value) (reflect.Value, error)

func (Func[Target, Source]) Info

func (Func[Target, Source]) Info() (string, string)

type MustFunc

type MustFunc[Target, Source any] func(Source) Target

func (MustFunc[Target, Source]) Convert

func (nc MustFunc[Target, Source]) Convert(cnv Converter, source reflect.Value) (reflect.Value, error)

func (MustFunc[Target, Source]) Info

func (MustFunc[Target, Source]) Info() (string, string)

type Option

type Option interface{}

type RecursiveFunc

type RecursiveFunc[Target, Source any] func(cnv Converter, src Source) (Target, error)

func (RecursiveFunc[Target, Source]) Convert

func (rf RecursiveFunc[Target, Source]) Convert(cnv Converter, source reflect.Value) (reflect.Value, error)

func (RecursiveFunc[Target, Source]) Info

func (RecursiveFunc[Target, Source]) Info() (string, string)

type StructFromStruct

type StructFromStruct[Target, Source any] map[string]string

StructFromStruct is a type that maps fields from a source struct to a target struct. The map keys are the target struct fields and the values are the source struct fields.

func (StructFromStruct[Target, Source]) Convert

func (fm StructFromStruct[Target, Source]) Convert(cnv Converter, source reflect.Value) (reflect.Value, error)

func (StructFromStruct[Target, Source]) Info

func (StructFromStruct[Target, Source]) Info() (string, string)

type ValueConverter

type ValueConverter interface {
	Info() (string, string)
	Convert(cnv Converter, source reflect.Value) (reflect.Value, error)
}

Jump to

Keyboard shortcuts

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