go_converter

package
v0.0.0-...-9c8aaae Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Copyright © 2019 hori-ryota <hori.ryota@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Example
package main

import (
	cmd "github.com/hori-ryota/go-codegen/codegen"
	"github.com/mattn/go-shellwords"
	"github.com/spf13/cobra"
)

func main() {
	input := "error protobuf go_converter -t ../../internal/testdata -p ../../internal/testdata/proto -o ../internal/testdata/converter"

	cmd := prepareCmdWithStdout(input)
	err := cmd.Execute()
	if err != nil {
		panic(err)
	}
}

func prepareCmdWithStdout(input string) *cobra.Command {
	input += " --useStdOut"
	args, err := shellwords.Parse(input)
	if err != nil {
		panic(err)
	}
	cmd := cmd.NewRootCmd()
	cmd.SetArgs(args)
	return cmd
}
Output:

// Code generated by go-codegen error protobuf go_converter; DO NOT EDIT

package converter

import (
	"github.com/hori-ryota/go-codegen/codegen/error/internal/testdata"
	"github.com/hori-ryota/go-codegen/codegen/error/internal/testdata/proto"
)

func ConvertErrorToProto(derr testdata.Error) *proto.Error {
	details := make([]*proto.ErrorDetail, len(derr.Details()))
	for i := range derr.Details() {
		details[i] = &proto.ErrorDetail{
			Code: derr.Details()[i].Code().String(),
			Args: derr.Details()[i].Args(),
		}
	}
	return &proto.Error{
		Code:    derr.Code().String(),
		Details: details,
	}
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var GoConverterTmpl = template.Must(template.New("GoConverterTmpl").Parse(`
// Code generated by go-codegen error protobuf go_converter; DO NOT EDIT

package {{ .PackageName }}

{{ .ImportPackages }}

func ConvertErrorToProto(derr {{ .ErrorPackagePrefix }}Error) *{{ .ProtoPackagePrefix }}Error {
	details := make([]*{{ .ProtoPackagePrefix }}ErrorDetail, len(derr.Details()))
	for i := range derr.Details() {
		details[i] = &{{ .ProtoPackagePrefix }}ErrorDetail{
			Code: derr.Details()[i].Code().String(),
			Args: derr.Details()[i].Args(),
		}
	}
	return &{{ .ProtoPackagePrefix }}Error{
		Code:    derr.Code().String(),
		Details: details,
	}
}
`))

Functions

func Generate

func Generate(
	errorDefinitionPkg *types.Package,
	protoPkg *types.Package,
	dstPackage *types.Package,
) (string, error)

func NewGoConverterCmd

func NewGoConverterCmd() *cobra.Command

func Run

func Run(
	errorDefinitionDir string,
	protoDir string,
	outputDir string,
	useStdOut bool,
) error

Types

type TmplParam

type TmplParam struct {
	PackageName        string
	ImportPackages     string
	ErrorPackagePrefix string
	ProtoPackagePrefix string
}

Jump to

Keyboard shortcuts

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