@@ -3,41 +3,46 @@ package main
33import (
44 "encoding/json"
55 "fmt"
6- "io/ioutil"
76 "log"
7+ "os"
88 "strings"
9+ "time"
910)
1011
11- var db map [string ]string
12+ type store map [string ]string
1213
14+ func (s store ) get (key string ) (string , error ) {
15+ time .Sleep (200 * time .Millisecond ) // simulate latency
16+ value , exists := s [key ]
17+ if ! exists {
18+ return "" , fmt .Errorf ("%s not found" , key )
19+ }
20+ return value , nil
21+ }
22+
23+ // emulates a database store
24+ var db store
25+
26+ // initialize the global db from the file
1327func init () {
14- f , err := ioutil .ReadFile ("db.json" )
28+ data , err := os .ReadFile ("db.json" )
1529 if err != nil {
1630 log .Fatal (err )
1731 }
18- if err := json .Unmarshal (f , & db ); err != nil {
32+ if err := json .Unmarshal (data , & db ); err != nil {
1933 log .Fatal (err )
2034 }
2135}
2236
23- func doETL (in []byte ) ([][]byte , error ) {
24- var res [][]byte
25- s := string (in )
26- if strings .HasPrefix (s , "+" ) {
27- key := strings .Replace (s , "+" , "" , 1 )
28- value , err := getByKey (key )
37+ func doETL (in string ) ([]string , error ) {
38+ var result []string
39+ key , found := strings .CutPrefix (in , "+" )
40+ if found {
41+ value , err := db .get (key )
2942 if err != nil {
3043 return nil , err
3144 }
32- return append (res , [] byte ( value ) ), nil
45+ return append (result , value ), nil
3346 }
34- return nil , nil
35- }
36-
37- func getByKey (key string ) (string , error ) {
38- value , exists := db [key ]
39- if ! exists {
40- return "" , fmt .Errorf ("%s not found" , key )
41- }
42- return value , nil
47+ return result , nil
4348}
0 commit comments