Cleanup kind's lessThan code generation

このコミットが含まれているのは:
Cory Slep 2019-01-04 21:56:29 +01:00
コミット 85ff299cf3
4個のファイルの変更21行の追加42行の削除

ファイルの表示

@ -554,23 +554,14 @@ func (p *FunctionalPropertyGenerator) singleTypeFuncs() []*codegen.Method {
)) ))
} }
// LessThan Method // LessThan Method
// LessFn is nil case -- call comparison Less method directly on the LHS lessCode := p.Kinds[0].lessFnCode(jen.Id(codegen.This()), jen.Id("o"))
// TODO: Move this logic to a Kind method (see nonfuncprop.go too)
lessCall := jen.Id(codegen.This()).Dot(compareLessMethod).Call(jen.Id("o"))
if p.Kinds[0].LessFn != nil {
// LessFn is indeed a function -- call this function
lessCall = p.Kinds[0].LessFn.Clone().Call(
jen.Id(codegen.This()),
jen.Id("o"),
)
}
methods = append(methods, codegen.NewCommentedValueMethod( methods = append(methods, codegen.NewCommentedValueMethod(
p.GetPrivatePackage().Path(), p.GetPrivatePackage().Path(),
compareLessMethod, compareLessMethod,
p.StructName(), p.StructName(),
[]jen.Code{jen.Id("o").Id(p.InterfaceName())}, []jen.Code{jen.Id("o").Id(p.InterfaceName())},
[]jen.Code{jen.Bool()}, []jen.Code{jen.Bool()},
[]jen.Code{jen.Return(lessCall)}, []jen.Code{jen.Return(lessCode)},
jen.Commentf("%s compares two instances of this property with an arbitrary but stable comparison.", compareLessMethod), jen.Commentf("%s compares two instances of this property with an arbitrary but stable comparison.", compareLessMethod),
)) ))
return methods return methods
@ -785,26 +776,11 @@ func (p *FunctionalPropertyGenerator) multiTypeFuncs() []*codegen.Method {
jen.Return(jen.False()), jen.Return(jen.False()),
)) ))
for i, kind := range p.Kinds { for i, kind := range p.Kinds {
// LessFn is nil case -- call comparison Less method directly on the LHS
// TODO: Move this logic to a Kind method (see nonfuncprop.go too)
if p.Kinds[i].LessFn == nil {
lessCode.Add( lessCode.Add(
jen.Else().If( jen.Else().If(
jen.Id(codegen.This()).Dot(p.isMethodName(i)).Call(), jen.Id(codegen.This()).Dot(p.isMethodName(i)).Call(),
).Block( ).Block(
jen.Return(jen.Id(codegen.This()).Dot(p.getFnName(i)).Call().Dot(compareLessMethod).Call(jen.Id("o").Dot(p.getFnName(i)).Call())))) jen.Return(kind.lessFnCode(jen.Id(codegen.This()).Dot(p.getFnName(i)).Call(), jen.Id("o").Dot(p.getFnName(i)).Call()))))
} else {
// LessFn is indeed a function -- call this function
lessCode.Add(
jen.Else().If(
jen.Id(codegen.This()).Dot(p.isMethodName(i)).Call(),
).Block(
kind.LessFn.Clone().Call(
jen.Id(codegen.This()).Dot(p.getFnName(i)).Call(),
jen.Id("o").Dot(p.getFnName(i)).Call(),
),
))
}
} }
methods = append(methods, codegen.NewCommentedValueMethod( methods = append(methods, codegen.NewCommentedValueMethod(
p.GetPrivatePackage().Path(), p.GetPrivatePackage().Path(),

ファイルの表示

@ -120,7 +120,6 @@ func NewManagerGenerator(pm PackageManager,
publicPkg := t.PublicPackage() publicPkg := t.PublicPackage()
mg.tgManagedMethods[t].ifaces = []*codegen.Interface{t.toInterface(publicPkg)} mg.tgManagedMethods[t].ifaces = []*codegen.Interface{t.toInterface(publicPkg)}
} }
// TODO: Move these back to pass 1
for _, p := range fp { for _, p := range fp {
publicPkg := p.GetPublicPackage() publicPkg := p.GetPublicPackage()
mg.fpManagedMethods[p].ifaces = []*codegen.Interface{p.toInterface(publicPkg)} mg.fpManagedMethods[p].ifaces = []*codegen.Interface{p.toInterface(publicPkg)}

ファイルの表示

@ -154,16 +154,7 @@ func (p *NonFunctionalPropertyGenerator) funcs() []*codegen.Method {
if i > 0 { if i > 0 {
less.Else() less.Else()
} }
// LessFn is nil case -- call comparison Less method directly on the LHS. lessCall := kind.lessFnCode(jen.Id("lhs"), jen.Id("rhs"))
// TODO: Move this logic to a Kind method (see funcprop.go too)
lessCall := jen.Id("lhs").Dot(compareLessMethod).Call(jen.Id("rhs"))
if kind.LessFn != nil {
// LessFn is indeed a function -- call this function
lessCall = kind.LessFn.Clone().Call(
jen.Id("lhs"),
jen.Id("rhs"),
)
}
less.If( less.If(
jen.Id("idx1").Op("==").Lit(i), jen.Id("idx1").Op("==").Lit(i),
).Block( ).Block(

ファイルの表示

@ -73,6 +73,19 @@ type Kind struct {
LessFn *jen.Statement LessFn *jen.Statement
} }
func (k Kind) lessFnCode(this, other *jen.Statement) *jen.Statement {
// LessFn is nil case -- call comparison Less method directly on the LHS
lessCall := this.Clone().Dot(compareLessMethod).Call(other.Clone())
if k.LessFn != nil {
// LessFn is indeed a function -- call this function
lessCall = k.LessFn.Clone().Call(
this.Clone(),
other.Clone(),
)
}
return lessCall
}
// PropertyGenerator is a common base struct used in both Functional and // PropertyGenerator is a common base struct used in both Functional and
// NonFunctional ActivityStreams properties. It provides common naming patterns, // NonFunctional ActivityStreams properties. It provides common naming patterns,
// logic, and common Go code to be generated. // logic, and common Go code to be generated.