Advertisement
yerden

group parse one-by-one

Nov 30th, 2022
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.72 KB | None | 0 0
  1. diff --git a/netunit/group.go b/netunit/group.go
  2. index 1cd6dd2..766aa44 100644
  3. --- a/netunit/group.go
  4. +++ b/netunit/group.go
  5. @@ -20,9 +20,10 @@ type Location struct {
  6.  
  7.  // Group represents netunit group descriptor.
  8.  type Group struct {
  9. -       Key      uint32 `json:"key,string"`
  10. -       Name     string `json:"name"`
  11. -       NetUnits []ULI  `json:"netunits"`
  12. +       Key         uint32   `json:"key,string"`
  13. +       Name        string   `json:"name"`
  14. +       NetUnitsRaw []string `json:"netunits"`
  15. +       NetUnits    []ULI
  16.  }
  17.  
  18.  // GroupsMap is a map of User Location Info to its net unit group ID.
  19. diff --git a/netunit/group_source.go b/netunit/group_source.go
  20. index a59e81d..e360439 100644
  21. --- a/netunit/group_source.go
  22. +++ b/netunit/group_source.go
  23. @@ -124,5 +124,25 @@ func (s *httpGroupSource) GetUnitGroupsByName(ctx context.Context, name string,
  24.                 return ErrGroupsNotFound
  25.         }
  26.  
  27. -       return json.NewDecoder(resp.Body).Decode(dst)
  28. +       if err := json.NewDecoder(resp.Body).Decode(dst); err != nil {
  29. +               return fmt.Errorf("unable to unmarshal group: %v", err)
  30. +       }
  31. +
  32. +       return dst.convertNetUnits()
  33. +}
  34. +
  35. +func (g *Group) convertNetUnits() error {
  36. +       g.NetUnits = g.NetUnits[:0]
  37. +
  38. +       for _, s := range g.NetUnitsRaw {
  39. +               var uli ULI
  40. +               if err := uli.UnmarshalString(s); err != nil {
  41. +                       fmt.Printf("unable to unmarshal location: '%s'\n", s)
  42. +                       continue
  43. +               }
  44. +
  45. +               g.NetUnits = append(g.NetUnits, uli)
  46. +       }
  47. +
  48. +       return nil
  49.  }
  50. diff --git a/netunit/location.go b/netunit/location.go
  51. index 15d3797..8e4ab3c 100644
  52. --- a/netunit/location.go
  53. +++ b/netunit/location.go
  54. @@ -59,23 +59,27 @@ func parseInts(s string, preAlloc int) ([]int, error) {
  55.  }
  56.  
  57.  // UnmarshalJSON implements json.Unmarshaler interface.
  58. -//
  59. -// It treats input data as 3GPP encoded locations.
  60.  func (u *ULI) UnmarshalJSON(data []byte) error {
  61.         var s string
  62. -       u.Locations = u.Locations[:0]
  63.  
  64.         if err := json.Unmarshal(data, &s); err != nil {
  65.                 return fmt.Errorf("unmarshal ULI in JSON %s: %w", data, err)
  66.         }
  67.  
  68. +       return u.UnmarshalString(s)
  69. +}
  70. +
  71. +// UnmarshalString unmarshals shitty text representation of location.
  72. +func (u *ULI) UnmarshalString(s string) error {
  73. +       u.Locations = u.Locations[:0]
  74. +
  75.         tokens, err := parseInts(s, 10)
  76.         if err != nil {
  77.                 return fmt.Errorf("parse ints %s: %w", s, err)
  78.         }
  79.  
  80.         if len(tokens) == 0 {
  81. -               return fmt.Errorf("no tokens in %s", data)
  82. +               return fmt.Errorf("no tokens in %s", s)
  83.         }
  84.  
  85.         switch tokens[0] {
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement