diff --git a/tools/exp/props/funcprop.go b/tools/exp/props/funcprop.go index ce75d2f..c419091 100644 --- a/tools/exp/props/funcprop.go +++ b/tools/exp/props/funcprop.go @@ -554,23 +554,14 @@ func (p *FunctionalPropertyGenerator) singleTypeFuncs() []*codegen.Method { )) } // LessThan Method - // LessFn is nil case -- call comparison Less method directly on the LHS - // 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"), - ) - } + lessCode := p.Kinds[0].lessFnCode(jen.Id(codegen.This()), jen.Id("o")) methods = append(methods, codegen.NewCommentedValueMethod( p.GetPrivatePackage().Path(), compareLessMethod, p.StructName(), []jen.Code{jen.Id("o").Id(p.InterfaceName())}, []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), )) return methods @@ -785,26 +776,11 @@ func (p *FunctionalPropertyGenerator) multiTypeFuncs() []*codegen.Method { jen.Return(jen.False()), )) 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( - jen.Else().If( - jen.Id(codegen.This()).Dot(p.isMethodName(i)).Call(), - ).Block( - jen.Return(jen.Id(codegen.This()).Dot(p.getFnName(i)).Call().Dot(compareLessMethod).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(), - ), - )) - } + lessCode.Add( + jen.Else().If( + jen.Id(codegen.This()).Dot(p.isMethodName(i)).Call(), + ).Block( + jen.Return(kind.lessFnCode(jen.Id(codegen.This()).Dot(p.getFnName(i)).Call(), jen.Id("o").Dot(p.getFnName(i)).Call())))) } methods = append(methods, codegen.NewCommentedValueMethod( p.GetPrivatePackage().Path(), diff --git a/tools/exp/props/manager.go b/tools/exp/props/manager.go index d198ab2..46ca77d 100644 --- a/tools/exp/props/manager.go +++ b/tools/exp/props/manager.go @@ -120,7 +120,6 @@ func NewManagerGenerator(pm PackageManager, publicPkg := t.PublicPackage() mg.tgManagedMethods[t].ifaces = []*codegen.Interface{t.toInterface(publicPkg)} } - // TODO: Move these back to pass 1 for _, p := range fp { publicPkg := p.GetPublicPackage() mg.fpManagedMethods[p].ifaces = []*codegen.Interface{p.toInterface(publicPkg)} diff --git a/tools/exp/props/nonfuncprop.go b/tools/exp/props/nonfuncprop.go index c1b13ce..14b561f 100644 --- a/tools/exp/props/nonfuncprop.go +++ b/tools/exp/props/nonfuncprop.go @@ -154,16 +154,7 @@ func (p *NonFunctionalPropertyGenerator) funcs() []*codegen.Method { if i > 0 { less.Else() } - // LessFn is nil case -- call comparison Less method directly on the LHS. - // 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"), - ) - } + lessCall := kind.lessFnCode(jen.Id("lhs"), jen.Id("rhs")) less.If( jen.Id("idx1").Op("==").Lit(i), ).Block( diff --git a/tools/exp/props/property.go b/tools/exp/props/property.go index d7ed297..ce8b2d3 100644 --- a/tools/exp/props/property.go +++ b/tools/exp/props/property.go @@ -73,6 +73,19 @@ type Kind struct { 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 // NonFunctional ActivityStreams properties. It provides common naming patterns, // logic, and common Go code to be generated.