go_definition

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 go_definition -t ../internal/testdata -o ../internal/testdata --codes Unknown,NotFound,BadRequest"

	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 go_definition; DO NOT EDIT

package testdata

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/hori-ryota/zaperr"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

type ErrorCode string

const (
	errorUnknown    ErrorCode = "Unknown"
	errorNotFound   ErrorCode = "NotFound"
	errorBadRequest ErrorCode = "BadRequest"
)

func (c ErrorCode) String() string {
	return string(c)
}

type Error interface {
	Error() string
	Code() ErrorCode
	Details() []ErrorDetail

	IsUnknown() bool
	IsNotFound() bool
	IsBadRequest() bool
}

func newError(source error, code ErrorCode, details ...ErrorDetail) Error {
	return errorImpl{
		source:  source,
		code:    code,
		details: details,
	}
}

func ErrorUnknown(source error, details ...ErrorDetail) Error {
	return newError(source, errorUnknown, details...)
}
func ErrorNotFound(source error, details ...ErrorDetail) Error {
	return newError(source, errorNotFound, details...)
}
func ErrorBadRequest(source error, details ...ErrorDetail) Error {
	return newError(source, errorBadRequest, details...)
}

type errorImpl struct {
	source  error
	code    ErrorCode
	details []ErrorDetail
}

func (e errorImpl) Error() string {
	return fmt.Sprintf("%s:%s:%s", e.code, e.details, e.source)
}
func (e errorImpl) Code() ErrorCode {
	return e.code
}
func (e errorImpl) Details() []ErrorDetail {
	return e.details
}

func (e errorImpl) IsUnknown() bool {
	return e.code == errorUnknown
}
func (e errorImpl) IsNotFound() bool {
	return e.code == errorNotFound
}
func (e errorImpl) IsBadRequest() bool {
	return e.code == errorBadRequest
}

type ErrorDetail struct {
	code ErrorDetailCode
	args []string
}

func newErrorDetail(code ErrorDetailCode, args ...string) ErrorDetail {
	return ErrorDetail{
		code: code,
		args: args,
	}
}

func (e ErrorDetail) String() string {
	return strings.Join(append([]string{e.code.String()}, e.args...), ",")
}

func (c ErrorDetail) Code() ErrorDetailCode {
	return c.code
}

func (c ErrorDetail) Args() []string {
	return c.args
}

type ErrorDetailCode string

func (c ErrorDetailCode) String() string {
	return string(c)
}

const ErrorDetailInvalidWorkKind ErrorDetailCode = "InvalidWorkKind"

func InvalidWorkKindError(
	workKind WorkKind,
) ErrorDetail {
	return newErrorDetail(
		ErrorDetailInvalidWorkKind,
		string(workKind),
	)
}

const ErrorDetailInvalidWorkingTime ErrorDetailCode = "InvalidWorkingTime"

func InvalidWorkingTimeError(
	startAt int64,
	endAt int64,
) ErrorDetail {
	return newErrorDetail(
		ErrorDetailInvalidWorkingTime,
		strconv.FormatInt(startAt, 10),
		strconv.FormatInt(endAt, 10),
	)
}

func (e errorImpl) MarshalLogObject(enc zapcore.ObjectEncoder) error {
	zaperr.ToNamedField("sourceError", e.source).AddTo(enc)
	zap.String("code", string(e.code)).AddTo(enc)
	zap.Any("details", e.details)
	return nil
}

Index

Examples

Constants

This section is empty.

Variables

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

package {{ .PackageName }}

{{ .ImportPackages }}

{{ $printer := .TypePrinter }}

type ErrorCode string

const (
	{{ range .ErrorCodes }}
	error{{ . }} ErrorCode = "{{ . }}"
	{{- end }}
)

func (c ErrorCode) String() string {
	return string(c)
}

type Error interface {
	Error() string
	Code() ErrorCode
	Details() []ErrorDetail
	{{ range .ErrorCodes }}
	Is{{ . }}() bool
	{{- end }}
}

func newError(source error, code ErrorCode, details ...ErrorDetail) Error {
	return errorImpl{
		source:  source,
		code:    code,
		details: details,
	}
}

{{ range .ErrorCodes }}
func Error{{ . }}(source error, details ...ErrorDetail) Error {
	return newError(source, error{{ . }}, details...)
}
{{- end }}

type errorImpl struct {
	source  error
	code    ErrorCode
	details []ErrorDetail
}

func (e errorImpl) Error() string {
	return fmt.Sprintf("%s:%s:%s", e.code, e.details, e.source)
}
func (e errorImpl) Code() ErrorCode {
	return e.code
}
func (e errorImpl) Details() []ErrorDetail {
	return e.details
}
{{ range .ErrorCodes }}
func (e errorImpl) Is{{ . }}() bool {
	return e.code == error{{ . }}
}
{{- end }}

type ErrorDetail struct {
	code ErrorDetailCode
	args []string
}

func newErrorDetail(code ErrorDetailCode, args ...string) ErrorDetail {
	return ErrorDetail{
		code: code,
		args: args,
	}
}

func (e ErrorDetail) String() string {
	return strings.Join(append([]string{e.code.String()}, e.args...), ",")
}

func (c ErrorDetail) Code() ErrorDetailCode {
	return c.code
}

func (c ErrorDetail) Args() []string {
	return c.args
}

type ErrorDetailCode string

func (c ErrorDetailCode) String() string {
	return string(c)
}

{{ range .DetailErrorCodes }}
const ErrorDetail{{ .Code }} ErrorDetailCode = "{{ .Code }}"
func {{ .Code }}Error(
	{{- range .Params }}
	{{ .Name }} {{ $printer.PrintRelativeType .Type }},
	{{- end }}
) ErrorDetail {
	return newErrorDetail(
		ErrorDetail{{ .Code }},
		{{- range .Params }}
		{{ $printer.ToStringConverter .Name .Type }},
		{{- end }}
	)
}
{{- end }}

func (e errorImpl) MarshalLogObject(enc zapcore.ObjectEncoder) error {
	zaperr.ToNamedField("sourceError", e.source).AddTo(enc)
	zap.String("code", string(e.code)).AddTo(enc)
	zap.Any("details", e.details)
	return nil
}
`))
View Source
var GodefTmplUsedPackages = []*types.Package{
	types.NewPackage("fmt", "fmt"),
	types.NewPackage("strconv", "strconv"),
	types.NewPackage("strings", "strings"),
	types.NewPackage("strings", "strings"),
	types.NewPackage("github.com/hori-ryota/zaperr", "zaperr"),
	types.NewPackage("go.uber.org/zap", "zap"),
	types.NewPackage("go.uber.org/zap/zapcore", "zapcore"),
}

Functions

func Generate

func Generate(pkgInfo *loader.PackageInfo, errorCodes []string, dstPackage *types.Package) (string, error)

func NewGoDefinitionCmd

func NewGoDefinitionCmd() *cobra.Command

func Run

func Run(targetDir string, codes []string, outputDir string, useStdOut bool) error

Types

type TmplParam

type TmplParam struct {
	PackageName      string
	ErrorCodes       []string
	DetailErrorCodes []internal.DetailErrorCodeInfo
	ImportPackages   string
	TypePrinter      typeutil.Printer
}

Jump to

Keyboard shortcuts

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