xwinie / juryrig

A tool for generating Go struct mapping code, inspired by mapstruct.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Photograph of a man fixing a truck.
A mechanic performing maintenance on a WWII truck.

JuryRig

A tool for generating Go struct mapping code, inspired by mapstruct.

StatusRunConfigurationContributingLicense

[GitHub release] [Build Status] GitHub go.mod Go version [Go Report Card] [License]

Status

JuryRig is in alpha - you can try it. :)

Run

Since JuryRig operates through go generate, once you've added the binary to your $PATH you can run

go generate ./...

... or similar.

Configuration

Given some structs...

type ExternalFilm struct {
    title   string
    runtime int
}

type ExternalUser struct {
    username string
    age      int
}

type InternalUser struct {
    username string
}

type InternalUserFilm struct {
    title   string
    runtime int
    director string
    user    InternalUser
}

We can describe a mapper as follows:

package film

//go:generate juryrig gen -o zz.mapper.impl.go

// +juryrig:mapper
type Mapper interface {
	// +juryrig:link:ef.title->title
	// +juryrig:link:ef.runtime->runtime
	// +juryrig:ignore:director
	// +juryrig:linkfunc:eu->ToInternalUser->user
	ToInternalUserFilm(ef ExternalFilm, eu ExternalUser) InternalUserFilm
	// +juryrig:link:eu.username->username
	ToInternalUser(eu ExternalUser) InternalUser
}

Running go generate will implement the following mapper struct in zz.mapper.impl.go:

package film

type MapperImpl struct{}

func (impl *MapperImpl) ToInternalUserFilm(ef ExternalFilm, eu ExternalUser) InternalUserFilm {
	return InternalUserFilm{
		title:   ef.title,
		runtime: ef.runtime,
		// director: (ignored),
		user: impl.ToInternalUser(eu),
	}
}

func (impl *MapperImpl) ToInternalUser(eu ExternalUser) InternalUser {
	return InternalUser{
		username: eu.username,
	}
}

Contributing

Please submit an issue with your proposal.

License

See LICENSE

About

A tool for generating Go struct mapping code, inspired by mapstruct.

License:MIT License


Languages

Language:Go 96.6%Language:Makefile 3.4%