@@ -137,44 +137,68 @@ Design 1: Functional code distributed within different package
137137Design 2: All functional code in one place
138138```
139139### Design 1: Functional code distributed within different package
140+ #### Generate functional code for struct - Employee
140141```
141- 1. Install "gofp" to generate code
142- go get github.com/logic-building/functional-go/gofp
143- go get -u github.com/logic-building/functional-go/gofp
144-
145- go install github.com/logic-building/functional-go/gofp
146-
147- 2. Add this line in a file where user defined data type exists
148- //go:generate gofp -destination <file> -pkg <pkg> -type <Types separated by comma>
149-
150- // If the user defined type has to be imported, then use options "-imports" and value will be comma separated.
151- // See the file internal/employer/employer.go for the example
152-
153- example:
154- package employee
155-
156- //go:generate gofp -destination fp.go -pkg employee -type "Employee, Teacher"
157142 type Employee struct {
158143 id int
159144 name string
160145 salary float64
161146 }
162-
163- type Teacher struct {
147+ ```
148+ #### 1. Add line given below in the file where struct resides
149+ ```
150+ //go:generate gofp -destination fp.go -pkg employee -type "Employee"
151+ ```
152+ ##### Example:
153+ ```
154+ //go:generate gofp -destination fp.go -pkg employee -type "Employee"
155+ type Employee struct {
164156 id int
165157 name string
166158 salary float64
167159 }
160+ ```
161+ ##### Note:
162+ ```
163+ //go:generate gofp -destination fp.go -pkg employee -type "Employee"
164+
165+ -destination fp.go : fp.go is a new file which contains functional code for struct - Employee
166+ -pkg employee : employee is package where struct "Employee" resides
167+ -type "Employee" : Employee is struct for which functional code is generated.
168168
169- Note:
170- A. fp.go : generated code
171- B. employee : package name
172- C. "Employee, Teacher" : User defined data types
173-
169+ ```
170+ #### Step 2. Install "gofp
171+ ```
172+ go get github.com/logic-building/functional-go/gofp
173+ go get -u github.com/logic-building/functional-go/gofp
174+ go install github.com/logic-building/functional-go/gofp
175+ ```
176+ #### Step 3. Run go generate from root folder of the project
177+ ```
178+ go generate ./...
179+ ```
180+ #### You are done. Enjoy the functional code
181+ ```
182+ emp1 := employee.Employee{1, "A", 1000}
183+ emp2 := employee.Employee{2, "B", 1000}
184+ emp3 := employee.Employee{3, "C", 1000}
185+
186+ empList := []employee.Employee{emp1, emp2, emp3}
187+
188+ newEmpList := employee.Map(incrementSalary, empList) // Returns: [{1 A 1500} {2 B 1500} {3 C 1500}]
189+
190+ func incrementSalary(emp employee.Employee) employee.Employee {
191+ emp.Salary = emp.Salary + 500
192+ return emp
193+ }
194+ ```
195+
196+ ##### Optional parameter
197+ ```
174198Options on go:generate :
175199 A: -only: overrides default behavior of generating all the functions. But it always includes Map and Filter
176200 //go:generate gofp -destination fp.go -pkg employee -type "Employee" -only "Distinct, DistinctPtr, DistinctP"
177- full-list-values-for-only: "DropLast, DropLastPtr,
201+ full-list-values-for-only: "Distinct, DistinctP, DropLast, DropLastPtr,
178202 DropWhile, DropWhileErr, DropWhilePtr, DropWhilePtrErr, Every, EveryErr, EveryPtr,
179203 EveryPtrErr, FilterMap, FilterMapErr, FilterMapPtr, FilterMapPtrErr,
180204 Remove, RemoveErr, RemovePtr, RemovePtrErr, Reduce, ReduceErr, ReducePtr, ReducePtrErr, Rest, RestPtr,
@@ -190,25 +214,6 @@ Options on go:generate :
190214 D. -mapfun: To generate Merge & Zip functions for struct
191215 //go:generate gofp -destination fp.go -pkg employee -type "Employee" -mapfun "true"
192216 Caution: It will complain at runtime if struct contains slice or array
193-
194- 3. Generate functional code
195- go generate ./...
196-
197- 4. Now write your code
198-
199- emp1 := employee.Employee{1, "A", 1000}
200- emp2 := employee.Employee{2, "B", 1000}
201- emp3 := employee.Employee{3, "C", 1000}
202-
203- empList := []employee.Employee{emp1, emp2, emp3}
204-
205- newEmpList := employee.Map(incrementSalary, empList) // Returns: [{1 A 1500} {2 B 1500} {3 C 1500}]
206-
207- func incrementSalary(emp employee.Employee) employee.Employee {
208- emp.Salary = emp.Salary + 500
209- return emp
210- }
211-
212217```
213218
214219### Design 2: All functional code in one place
0 commit comments