gocnv

module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT

README

Gocnv – Go Converter Generator

Gocnv is a Go code generation tool that creates type conversion helper methods based on user defined templates.

TODO

  • Add Tests

Installation

go install github.com/AdamShannag/gocnv/cmd/gocnv@latest

Or run directly:

go run github.com/AdamShannag/gocnv/cmd/gocnv@latest -c ./path/to/gocnv.yaml

YAML Configuration Example

package: converter
dir: ./generated/converter

types:
  - name: String
    go: string
  - name: Int16
    go: int16

templates:
  basic: |
    func To{{.Conv}}{{.Type}}{{.Suffix}}(v {{.ArgType}}) {{.Pkg}}.Val[{{.ArgType}}] {
    	return {{.Pkg}}.From(v)
    }

converters:
  - name: Omit
    import: github.com/aarondl/opt/omit
    package: omit
    templates: [ basic ]
    generateNullable: true

You can also check testdata/gocnv.yaml for a complete example.

Configuration Reference

Field Description
package Go package name for generated files
dir Output directory for generated files
types List of Go types to generate converters for
templates Named templates for generating methods
converters List of converters to generate
converters.name Name used in generated method names
converters.import Go package import path
converters.package Alias to use in the generated code
converters.templates Templates to use for this converter
converters.generateNullable Generate pointer variants if true
converters.types / excludeTypes Optional type filters (include/exclude specific types)
converters.ReturnTypes Optional list of return types; generates all combinations if specified

Example Generated Code

// Code generated by gocnv. DO NOT EDIT.

package gocnv

import (
	omit "github.com/aarondl/opt/omit"
)

func ToOmitSliceString(v []string) omit.Val[[]string] {
	return omit.From(v)
}

func ToOmitSliceStringPtr(v *[]string) omit.Val[*[]string] {
	return omit.From(v)
}

func ToOmitInt16(v int16) omit.Val[int16] {
	return omit.From(v)
}

func ToOmitInt16Ptr(v *int16) omit.Val[*int16] {
	return omit.From(v)
}

License

MIT

Contribution

If you have any questions or want to contribute, feel free to open an issue or PR.

Directories

Path Synopsis
cmd
gocnv command
internal
gen

Jump to

Keyboard shortcuts

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