activity/tools/exp/codegen/method.go

209 行
4.5 KiB
Go
Raw 通常表示 履歴

2018-10-20 05:44:13 +09:00
package codegen
2018-10-09 05:19:10 +09:00
import (
"github.com/dave/jennifer/jen"
)
type memberType int
const (
this = "this"
)
const (
valueMember memberType = iota
pointerMember
)
2018-10-10 07:32:37 +09:00
// This returns the string variable used by members to refer to themselves.
2018-10-09 05:19:10 +09:00
func This() string {
return this
}
2018-10-10 07:32:37 +09:00
// Function represents a free function, not a method, for Go code to be
// generated.
2018-10-09 05:19:10 +09:00
type Function struct {
qual *jen.Statement
name string
params []jen.Code
ret []jen.Code
block []jen.Code
comment jen.Code
}
2018-10-10 07:32:37 +09:00
// NewCommentedFunction creates a new function with a comment.
2018-10-09 05:19:10 +09:00
func NewCommentedFunction(pkg, name string,
params, ret, block []jen.Code,
comment jen.Code) *Function {
return &Function{
qual: jen.Qual(pkg, name),
name: name,
params: params,
ret: ret,
block: block,
comment: comment,
}
}
2018-10-10 07:32:37 +09:00
// NewFunction creates a new function without any comments.
2018-10-09 05:19:10 +09:00
func NewFunction(pkg, name string,
params, ret, block []jen.Code) *Function {
return &Function{
qual: jen.Qual(pkg, name),
name: name,
params: params,
ret: ret,
block: block,
comment: nil,
}
}
2018-10-10 07:32:37 +09:00
// Definition generates the Go code required to define and implement this
// function.
2018-10-09 05:19:10 +09:00
func (m Function) Definition() jen.Code {
stmts := jen.Empty()
if m.comment != nil {
stmts = jen.Empty().Add(m.comment).Line()
}
return stmts.Add(jen.Func().Id(m.name).Params(
m.params...,
).Params(
m.ret...,
).Block(
m.block...,
))
}
2018-10-10 07:32:37 +09:00
// Call generates the Go code required to call this function, with qualifier if
// required.
2018-10-09 05:19:10 +09:00
func (m Function) Call(params ...jen.Code) jen.Code {
2018-10-20 05:44:13 +09:00
return m.qual.Clone().Call(params...)
2018-10-09 05:19:10 +09:00
}
2018-10-10 07:32:37 +09:00
// Name returns the identifier of this function.
2018-10-09 05:19:10 +09:00
func (m Function) Name() string {
return m.name
}
2018-10-10 07:32:37 +09:00
// Method represents a method on a type, not a free function, for Go code to be
// generated.
2018-10-09 05:19:10 +09:00
type Method struct {
member memberType
structName string
function *Function
}
2018-10-10 07:32:37 +09:00
// NewCommentedValueMethod defines a commented method for the value of a type.
2018-10-09 05:19:10 +09:00
func NewCommentedValueMethod(pkg, name, structName string,
params, ret, block []jen.Code,
comment jen.Code) *Method {
return &Method{
member: valueMember,
structName: structName,
function: &Function{
qual: jen.Qual(pkg, name),
name: name,
params: params,
ret: ret,
block: block,
comment: comment,
},
}
}
2018-10-10 07:32:37 +09:00
// NewValueMethod defines a method for the value of a type. It is not commented.
2018-10-09 05:19:10 +09:00
func NewValueMethod(pkg, name, structName string,
params, ret, block []jen.Code) *Method {
return &Method{
member: valueMember,
structName: structName,
function: &Function{
qual: jen.Qual(pkg, name),
name: name,
params: params,
ret: ret,
block: block,
comment: nil,
},
}
}
2018-10-10 07:32:37 +09:00
// NewCommentedPointerMethod defines a commented method for the pointer to a
// type.
2018-10-09 05:19:10 +09:00
func NewCommentedPointerMethod(pkg, name, structName string,
params, ret, block []jen.Code,
comment jen.Code) *Method {
return &Method{
member: pointerMember,
structName: structName,
function: &Function{
qual: jen.Qual(pkg, name),
name: name,
params: params,
ret: ret,
block: block,
comment: comment,
},
}
}
2018-10-10 07:32:37 +09:00
// NewPointerMethod defines a method for the pointer to a type. It is not
// commented.
2018-10-09 05:19:10 +09:00
func NewPointerMethod(pkg, name, structName string,
params, ret, block []jen.Code) *Method {
return &Method{
member: pointerMember,
structName: structName,
function: &Function{
qual: jen.Qual(pkg, name),
name: name,
params: params,
ret: ret,
block: block,
comment: nil,
},
}
}
2018-10-10 07:32:37 +09:00
// Definition generates the Go code required to define and implement this
// method.
2018-10-09 05:19:10 +09:00
func (m Method) Definition() jen.Code {
comment := jen.Empty()
if m.function.comment != nil {
comment = jen.Empty().Add(m.function.comment).Line()
}
funcDef := jen.Empty()
switch m.member {
case pointerMember:
funcDef = jen.Func().Params(
jen.Id(This()).Op("*").Id(m.structName),
)
case valueMember:
funcDef = jen.Func().Params(
jen.Id(This()).Id(m.structName),
)
default:
panic("unhandled method memberType")
}
return comment.Add(funcDef.Id(
m.function.name,
).Params(
m.function.params...,
).Params(
m.function.ret...,
).Block(
m.function.block...,
))
}
2018-10-10 07:32:37 +09:00
// Call generates the Go code required to call this method, with qualifier if
// required.
2018-10-09 05:19:10 +09:00
func (m Method) Call(on string, params ...jen.Code) jen.Code {
return jen.Id(on).Call(params...)
}
2018-10-10 07:32:37 +09:00
// Name returns the identifier of this function.
2018-10-09 05:19:10 +09:00
func (m Method) Name() string {
return m.function.name
}