Advertisement
Vassa007

Untitled

Jan 12th, 2023
1,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.58 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/csv"
  5.     "encoding/json"
  6.     "fmt"
  7.     "io"
  8.     "log"
  9.     "os"
  10.     "path/filepath"
  11.     "strings"
  12.     "sync"
  13. )
  14.  
  15. // Route contains route information
  16. type Route struct {
  17.     Name   string
  18.     Points []Point
  19. }
  20.  
  21. // Point represent route point
  22. type Point struct {
  23.     Longitude float64 `json:"lng"`
  24.     Latitude  float64 `json:"lat"`
  25. }
  26.  
  27. // Reader defines functionality route reader / parser
  28. type Reader interface {
  29.     Read(io.Reader) ([]Point, error)
  30. }
  31.  
  32. // CreateReader is factory to create route reader
  33. type CreateReader func() Reader
  34.  
  35. var (
  36.     readers    map[string]CreateReader
  37.     readerLock sync.Once
  38. )
  39.  
  40. // RegisterReader add new route reader factory to list
  41. func RegisterReader(name string, factory CreateReader) {
  42.     readerLock.Do(func() {
  43.         readers = make(map[string]CreateReader)
  44.     })
  45.  
  46.     readers[name] = factory
  47. }
  48.  
  49. // GetReader create reader base on given factory name
  50. func GetReader(name string) Reader {
  51.     factory, ok := readers[name]
  52.  
  53.     log.Println("Hayoo", readers[name])
  54.  
  55.     if !ok {
  56.         fmt.Printf("%s is registered readers\n", name)
  57.         return nil
  58.     }
  59.  
  60.     return factory()
  61. }
  62.  
  63. func parseRouteFile(file string, reader Reader) (Route, error) {
  64.     f, err := os.Open(file)
  65.     if err != nil {
  66.         return Route{}, err
  67.     }
  68.  
  69.     var coordinates []Point
  70.  
  71.     switch filepath.Ext(file) {
  72.     case ".json":
  73.         log.Println("iki json")
  74.  
  75.         defer f.Close()
  76.         coordinates, err := reader.Read(f)
  77.         if err != nil {
  78.             return Route{}, err
  79.         }
  80.  
  81.         dataContent, err := io.ReadAll(f)
  82.  
  83.         err = json.Unmarshal(dataContent, &coordinates)
  84.         if err != nil {
  85.             log.Fatal("ERROR :", err)
  86.         }
  87.  
  88.     case ".csv":
  89.         log.Println("iki csv")
  90.         defer f.Close()
  91.  
  92.         csvReader := csv.NewReader(f)
  93.         coordinates, err := csvReader.ReadAll()
  94.         if err != nil {
  95.             log.Fatal(err)
  96.         }
  97.  
  98.         for _, line := range coordinates {
  99.             log.Println("HANTU : ", &line)
  100.         }
  101.  
  102.     }
  103.  
  104.     return Route{
  105.         Name:   filepath.Base(file),
  106.         Points: coordinates,
  107.     }, nil
  108.  
  109. }
  110.  
  111. // parseRoutes lookup routes files on given root path
  112. // read and parse into Routes
  113. func parseRoutes(root string) []Route {
  114.     var routes []Route
  115.  
  116.     filepath.Walk(root, func(p string, i os.FileInfo, err error) error {
  117.         if err == nil && !i.IsDir() {
  118.             // get file extension and check whether
  119.             // route file is supported
  120.  
  121.             //log.Println("found >> ", filepath.Base(p))
  122.  
  123.             reader := GetReader(strings.TrimLeft(filepath.Ext(p), "."))
  124.             log.Println("MATAMU ", root)
  125.             if reader != nil {
  126.                 route, err := parseRouteFile(p, reader)
  127.                 if err == nil {
  128.                     routes = append(routes, route)
  129.                 }
  130.             }
  131.  
  132.         }
  133.  
  134.         return nil
  135.     })
  136.  
  137.     return routes
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement