gotypeconverter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: Apache-2.0 Imports: 20 Imported by: 0

README

gotypeconverter

gotypeconverter generates a function that converts two structurally different types.

Install

go get github.com/fuji8/gotypeconverter/cmd/type-converter

Usage

> type-converter                          
gotypeconverter: gotypeconverter generates a function that converts two different named types.

Usage: gotypeconverter [-flag] [package]


Flags:
  -d string
        destination struct
  -o string
        output file; if nil, output stdout
  -s string
        source struct

Examples

see testdata

Basic example
package basic

type basicSrc struct {
	foo int
	bar float64
	x   string
	y   string
}

type basicDst struct {
	foo int
	bar float32
	x   string
	y   bool
}
> type-converter -s basicSrc -d basicDst .
// Code generated by gotypeconverter; DO NOT EDIT.
package basic

func ConvertbasicSrcTobasicDst(src basicSrc) (dst basicDst) {
        dst.foo = src.foo
        dst.x = src.x
        return
}
Normal example
package normal

type e struct {
	e       bool
	m       string
	x       int
	members []uint8
}

type ug struct {
	uaiueo uint8
	gaaaa  uint8
}

type normalSrc struct {
	x struct {
		A int
		B bool
		C string
		D string
	}
	y int
	z float64
	m string

	members []ug
}

type normalDst struct {
	e
	x string
	y int
	z float64
}
> type-converter -s normalSrc -d normalDst .                                
// Code generated by gotypeconverter; DO NOT EDIT.
package normal

func ConvertnormalSrcTonormalDst(src normalSrc) (dst normalDst) {
        dst.e.m = src.m
        dst.e.x = src.x.A
        dst.e.members = make([]uint8, len(src.members))
        for i := range src.members {
                dst.e.members[i] = src.members[i].uaiueo
        }
        dst.x = src.x.C
        dst.y = src.y
        dst.z = src.z
        return
}

Conversion rules

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Generator = &codegen.Generator{
	Name: "gotypeconverter",
	Doc:  doc,
	Run:  run,
}

Functions

func CreateTmpFile

func CreateTmpFile(path string)

func Init

func Init()

Init 解析のための一時ファイルを作成する

Types

type FuncMaker

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

FuncMaker generate function

func (*FuncMaker) MakeFunc

func (fm *FuncMaker) MakeFunc(dstType, srcType types.Type)

MakeFunc make function TODO fix only named type

func (*FuncMaker) WriteBytes

func (fm *FuncMaker) WriteBytes() (out []byte)

WriteBytes 全ての関数を書き出す。

Directories

Path Synopsis
cmd
type-converter command

Jump to

Keyboard shortcuts

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