-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctional.go
89 lines (73 loc) · 1.87 KB
/
functional.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"errors"
"fmt"
)
var employees = map[int]interface{} {
1: map[string]string {
"Role": "D",
"Language" : "Go",
},
2: map[string]string {
"Role": "T",
"Framework" : "Selenium",
},
3: map[string]string {
"Role": "P",
"Methodology": "Agile",
},
}
type EmployeeRole int
const (
Developer EmployeeRole = iota
Tester
ProjectManager
)
var roleMap = map[string]EmployeeRole {
"D" : Developer,
"T" : Tester,
"P" : ProjectManager,
}
type PrintFunction func(map[string]string)
var printFunctionMap = map[EmployeeRole]PrintFunction {
Developer: PrintDeveloper,
Tester: PrintTester,
ProjectManager: PrintProjectManager,
}
func PrintDeveloper(userData map[string]string) {
fmt.Println("Developer likes to code in " + userData["Language"])
}
func PrintTester(userData map[string]string) {
fmt.Println("Tester likes to test with " + userData["Framework"])
}
func PrintProjectManager(userData map[string]string) {
fmt.Println("Project manager like to deliver using " + userData["Methodology"]);
}
type LoadUserFunction func(int) (map[string]string, error)
func LoadUser(id int) (map[string]string, error) {
if employee, validId := employees[id]; !validId {
return nil, errors.New("Invalid employee id")
} else {
employeeMap := make(map[string]string, 2)
for k, v := range employee.(map[string]string) {
employeeMap[k] = v
}
return employeeMap, nil
}
}
type PrintUserFunction func (map[string]string) ()
func PrintUser(employee map[string]string) {
roleAbbreviation := employee["Role"]
role := roleMap[roleAbbreviation]
printFunctionMap[role](employee)
}
func LoadAndPrint(employeeId int, loadUser LoadUserFunction, printUser PrintUserFunction) {
if employee, loadError := loadUser(employeeId); loadError != nil {
fmt.Println(loadError.Error())
} else {
printUser(employee)
}
}
func main() {
LoadAndPrint(1, LoadUser, PrintUser)
}