Skip to content

Setting: variables

variables accepts no arguments and can be defined as conversion comment.

variables instructs goverter to generate an implementation for the given variables. You can have multiple variables blocks in one package.

See output to control the output location/package of the generated converter.

go
package house

import (
	"database/sql"

	"github.com/jmattheis/goverter/example/format/common"
)

// goverter:variables
// goverter:output:format assign-variable
// goverter:extend SQLStringToPString
// goverter:extend ConvertToApartmentMap
var (
	ConvertHouse func(source common.DBHouse) common.APIHouse
	// goverter:map Name FirstName
	// goverter:ignore Age
	ConvertPerson func(source common.DBPerson) common.APIPerson
	// goverter:map Owner.Name OwnerName
	ConvertApartment func(source common.DBApartment) common.APIApartment
)

func SQLStringToPString(value sql.NullString) *string {
	if value.Valid {
		return &value.String
	}
	return nil
}

func ConvertToApartmentMap(source []common.DBApartment) map[uint]common.APIApartment {
	m := make(map[uint]common.APIApartment)
	for _, apartment := range source {
		m[apartment.Position] = ConvertApartment(apartment) // !! this is not supported in some formats
	}
	return m
}
go
package common

import "database/sql"

type DBHouse struct {
	Address    string
	Apartments []DBApartment
}

type DBApartment struct {
	Position   uint
	Owner      DBPerson
	CoResident []DBPerson
}

type DBPerson struct {
	ID         int
	Name       string
	MiddleName sql.NullString
	Friends    []DBPerson
	Info       Info
}

type APIHouse struct {
	Address    string
	Apartments map[uint]APIApartment
}

type APIApartment struct {
	Position   uint
	Owner      APIPerson
	OwnerName  string
	CoResident []APIPerson
}

type APIPerson struct {
	ID         int
	MiddleName *string
	FirstName  *string
	Friends    []APIPerson
	Info       Info
	Age        int
}

type Info struct {
	Birthplace string
}
go
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
//go:build !goverter

package house

import common "github.com/jmattheis/goverter/example/format/common"

func init() {
	ConvertApartment = func(source common.DBApartment) common.APIApartment {
		var commonAPIApartment common.APIApartment
		commonAPIApartment.Position = source.Position
		commonAPIApartment.Owner = ConvertPerson(source.Owner)
		commonAPIApartment.OwnerName = source.Owner.Name
		if source.CoResident != nil {
			commonAPIApartment.CoResident = make([]common.APIPerson, len(source.CoResident))
			for i := 0; i < len(source.CoResident); i++ {
				commonAPIApartment.CoResident[i] = ConvertPerson(source.CoResident[i])
			}
		}
		return commonAPIApartment
	}
	ConvertHouse = func(source common.DBHouse) common.APIHouse {
		var commonAPIHouse common.APIHouse
		commonAPIHouse.Address = source.Address
		commonAPIHouse.Apartments = ConvertToApartmentMap(source.Apartments)
		return commonAPIHouse
	}
	ConvertPerson = func(source common.DBPerson) common.APIPerson {
		var commonAPIPerson common.APIPerson
		commonAPIPerson.ID = source.ID
		commonAPIPerson.MiddleName = SQLStringToPString(source.MiddleName)
		pString := source.Name
		commonAPIPerson.FirstName = &pString
		if source.Friends != nil {
			commonAPIPerson.Friends = make([]common.APIPerson, len(source.Friends))
			for i := 0; i < len(source.Friends); i++ {
				commonAPIPerson.Friends[i] = ConvertPerson(source.Friends[i])
			}
		}
		commonAPIPerson.Info = commonInfoToCommonInfo(source.Info)
		return commonAPIPerson
	}
}
func commonInfoToCommonInfo(source common.Info) common.Info {
	var commonInfo common.Info
	commonInfo.Birthplace = source.Birthplace
	return commonInfo
}