Skip to content

Commit 0bf0595

Browse files
author
Sah, Nandeshwar
committed
Added function - 'Remove' in the method chain
1 parent f848d85 commit 0bf0595

File tree

10 files changed

+1432
-30
lines changed

10 files changed

+1432
-30
lines changed

fp/methodchain.go

Lines changed: 448 additions & 28 deletions
Large diffs are not rendered by default.

fp/methodchain_test.go

Lines changed: 540 additions & 0 deletions
Large diffs are not rendered by default.

gofp/gofp.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,17 @@ func generateFPCode(pkg, dataTypes, imports string, structToFieldsMapUnexpected
334334
if fp.ExistsStrIgnoreCase("Remove", onlyList) {
335335
template += template2.Remove()
336336
template = r.Replace(template)
337+
338+
template += template3.MethodChainStructForRemove()
339+
template = r3.Replace(template)
337340
}
338341

339342
if fp.ExistsStrIgnoreCase("RemovePtr", onlyList) {
340343
template += template2.RemovePtr()
341344
template = r.Replace(template)
345+
346+
template += template3.MethodChainStructForRemovePtr()
347+
template = r3.Replace(template)
342348
}
343349

344350
if fp.ExistsStrIgnoreCase("RemovePtrErr", onlyList) {
@@ -824,6 +830,12 @@ func generateFPCode(pkg, dataTypes, imports string, structToFieldsMapUnexpected
824830
template += template3.MethodChainStruct()
825831
template = r3.Replace(template)
826832

833+
template += template3.MethodChainStructForRemove()
834+
template = r3.Replace(template)
835+
836+
template += template3.MethodChainStructForRemovePtr()
837+
template = r3.Replace(template)
838+
827839
// if struct's has member of type other than basic types such as list then use template which uses reflect
828840
if len(complextFieldsInStructSuchAsSlice) > 1 {
829841
switchTemplateDistinct = true

internal/employee/fp.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/employee/fp_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,61 @@ func TestMethodChainWithMapPtr(t *testing.T) {
505505
t.Errorf("error in method chain with Map %v %v", employees[0].Salary, employeesWithIncrementedSalary[0].Salary)
506506
}
507507
}
508+
509+
func TestMethodChainWithRemove(t *testing.T) {
510+
now := time.Now()
511+
employees := []Employee{
512+
{Id: 1, Name: "Ram", Salary: 700, CreationDate: now},
513+
{Id: 2, Name: "Shyam", Salary: 800, CreationDate: now},
514+
{Id: 2, Name: "Shyam", Salary: 800, CreationDate: now},
515+
{Id: 3, Name: "Radha", Salary: 900, CreationDate: now}}
516+
517+
salaryIncrement := func(emp Employee) Employee {
518+
emp.Salary = emp.Salary + 1000
519+
return emp
520+
}
521+
522+
salaryGreaterThan700 := func(emp Employee) bool {
523+
return emp.Salary > 700
524+
}
525+
526+
salaryEqualTo800 := func(emp Employee) bool {
527+
return emp.Salary == 800
528+
}
529+
employeesWithIncrementedSalary := MakeEmployeeSlice(employees...).
530+
Filter(salaryGreaterThan700).
531+
Remove(salaryEqualTo800).
532+
Map(salaryIncrement)
533+
if employees[3].Salary+1000 != employeesWithIncrementedSalary[0].Salary {
534+
t.Error("error in method chain with Map", employees)
535+
}
536+
}
537+
538+
func TestMethodChainWithRemovePtr(t *testing.T) {
539+
now := time.Now()
540+
employees := []*Employee{
541+
{Id: 1, Name: "Ram", Salary: 700, CreationDate: now},
542+
{Id: 2, Name: "Shyam", Salary: 800, CreationDate: now},
543+
{Id: 2, Name: "Shyam", Salary: 800, CreationDate: now},
544+
{Id: 3, Name: "Radha", Salary: 900, CreationDate: now}}
545+
546+
salaryIncrement := func(emp *Employee) *Employee {
547+
emp.Salary = emp.Salary + 1000
548+
return emp
549+
}
550+
551+
salaryGreaterThan700 := func(emp *Employee) bool {
552+
return emp.Salary > 700
553+
}
554+
555+
salaryEqualTo800 := func(emp *Employee) bool {
556+
return emp.Salary == 800
557+
}
558+
employeesWithIncrementedSalary := MakeEmployeeSlicePtr(employees...).
559+
FilterPtr(salaryGreaterThan700).
560+
RemovePtr(salaryEqualTo800).
561+
MapPtr(salaryIncrement)
562+
if 1900 != employeesWithIncrementedSalary[0].Salary {
563+
t.Error("error in method chain with Map", employees)
564+
}
565+
}

internal/employee/teacherFP.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/employer/fp.go

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/gfp/fp.go

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)