-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (72 loc) · 1.59 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
)
func fetchAPI(done <-chan struct{}) <-chan string {
out := make(chan string)
go func() {
defer close(out)
data := []string{
`{"id": 1, "name": "Alice", "status": "active"}`,
`{"id": 2, "name": "Bob", "status": "inactive"}`,
`{"id": 3, "name": "Charlie", "status": "active"}`,
}
for _, v := range data {
select {
case out <- v:
case <-done:
return
}
}
}()
return out
}
func parseJson(done <-chan struct{}, in <-chan string) <-chan map[string]interface{} {
out := make(chan map[string]interface{})
go func() {
defer close(out)
for raw := range in {
var result map[string]interface{}
if err := json.Unmarshal([]byte(raw), &result); err != nil {
fmt.Println("Error parsing JSON:", err)
continue
}
select {
case out <- result:
case <-done:
return
}
}
}()
return out
}
func filterData(done <-chan struct{}, in <-chan map[string]interface{}) <-chan map[string]interface{} {
out := make(chan map[string]interface{})
go func() {
defer close(out)
for item := range in {
if status, ok := item["status"].(string); ok && status == "active" {
select {
case out <- item:
case <-done:
return
}
}
}
}()
return out
}
func saveToDB(done <-chan struct{}, in <-chan map[string]interface{}) {
for item := range in {
fmt.Println("Saving to DB:", item)
}
}
func main() {
done := make(chan struct{})
defer close(done)
fetchedData := fetchAPI(done)
parseData := parseJson(done, fetchedData)
filteredData := filterData(done, parseData)
saveToDB(done, filteredData)
}