go-struct-converter – github.com/anchore/go-struct-converter Index | Files

package converter

import "github.com/anchore/go-struct-converter"

Index

Functions

func Convert

func Convert(from interface{}, to interface{}) error

Convert takes two objects, e.g. v2_1.Document and &v2_2.Document{} and attempts to map all the properties from one to the other. After the automatic mapping, if a struct implements the ConvertFrom interface, this is called to perform any additional conversion logic necessary.

Types

type Chain

type Chain struct {
	Types []reflect.Type
}

Chain holds a set of types with which to migrate through when a `chain.Convert` call is made

func NewChain

func NewChain(structs ...interface{}) Chain

NewChain takes a set of structs, in order, to allow for accurate chain.Convert(from, &to) calls. NewChain should be called with struct values in a manner similar to this: converter.NewChain(v1.Document{}, v2.Document{}, v3.Document{})

func (Chain) Convert

func (c Chain) Convert(from interface{}, to interface{}) (err error)

Convert converts from one type in the chain to the target type, calling each conversion in between

type ConvertFrom

type ConvertFrom interface {
	ConvertFrom(interface{}) error
}

ConvertFrom interface allows structs to define custom conversion functions if the automated reflection-based Convert is not able to convert properties due to name changes or other factors.

Source Files

chain.go converter.go

Version
v0.0.0-20250211213226-cce56d595160 (latest)
Published
Feb 11, 2025
Platform
linux/amd64
Imports
3 packages
Last checked
2 weeks ago

Tools for package owners.