Skip to content

Commit 64755f7

Browse files
authored
Merge pull request #177 from HyperloopUPV-H8/backend/array-to-map
[backend][3] ADJ alpha.1 - Add array-to-map logic
2 parents c7a08db + 5a03b3f commit 64755f7

4 files changed

Lines changed: 56 additions & 11 deletions

File tree

backend/internal/adj/adj.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package adj
33
import (
44
"encoding/json"
55
"errors"
6+
"github.com/HyperloopUPV-H8/h9-backend/internal/utils"
67
"github.com/go-git/go-git/v5"
78
"os"
89
)
@@ -18,11 +19,17 @@ func NewADJ() (*ADJ, error) {
1819
return nil, err
1920
}
2021

22+
var JsonInfo JSON_Info
2123
var info Info
22-
if err := json.Unmarshal(infoRaw, &info); err != nil {
24+
if err := json.Unmarshal(infoRaw, &JsonInfo); err != nil {
2325
return nil, err
2426
}
2527

28+
info.Ports = utils.ConvertToMap_Str_Uint16(JsonInfo.Ports)
29+
info.Addresses = utils.ConvertToMap_Str_Str(JsonInfo.Addresses)
30+
info.Units = utils.ConvertToMap_Str_Op(JsonInfo.Units)
31+
info.MessageIds = utils.ConvertToMap_Str_Uint16(JsonInfo.MessageIds)
32+
2633
var boardsList map[string]string
2734
if err := json.Unmarshal(boardsRaw, &boardsList); err != nil {
2835
return nil, err

backend/internal/adj/models.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ type ADJ struct {
77
Boards map[string]Board
88
}
99

10-
type Info struct {
10+
type JSON_Info struct {
1111
Ports []map[string]uint16 `json:"ports"`
1212
Addresses []map[string]string `json:"addresses"`
1313
Units []map[string]utils.Operations `json:"units"`
1414
MessageIds []map[string]uint16 `json:"message_ids"`
15-
BoardIds []map[string]uint16
15+
}
16+
17+
type Info struct {
18+
Ports map[string]uint16
19+
Addresses map[string]string
20+
Units map[string]utils.Operations
21+
MessageIds map[string]uint16
22+
BoardIds map[string]uint16
1623
}
1724

1825
type Board struct {

backend/internal/pod_data/models.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ type Measurement interface {
2828
}
2929

3030
type NumericMeasurement struct {
31-
Id string `json:"id"` // Remove json tags
32-
Name string `json:"name"`
33-
Type string `json:"type"`
34-
Units []string `json:"units"`
35-
DisplayUnits []utils.Units `json:"-"`
36-
PodUnits []utils.Units `json:"-"`
37-
SafeRange []*float64 `json:"safeRange"`
38-
WarningRange []*float64 `json:"warningRange"`
31+
Id string `json:"id"` // Remove json tags
32+
Name string `json:"name"`
33+
Type string `json:"type"`
34+
Units string `json:"units"`
35+
DisplayUnits utils.Units `json:"-"`
36+
PodUnits utils.Units `json:"-"`
37+
SafeRange []*float64 `json:"safeRange"`
38+
WarningRange []*float64 `json:"warningRange"`
3939
}
4040

4141
func (m NumericMeasurement) GetId() string {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package utils
2+
3+
func ConvertToMap_Str_Uint16(array []map[string]uint16) map[string]uint16 {
4+
result := make(map[string]uint16)
5+
for _, item := range array {
6+
for k, v := range item {
7+
result[k] = v
8+
}
9+
}
10+
return result
11+
}
12+
13+
func ConvertToMap_Str_Str(array []map[string]string) map[string]string {
14+
result := make(map[string]string)
15+
for _, item := range array {
16+
for k, v := range item {
17+
result[k] = v
18+
}
19+
}
20+
return result
21+
}
22+
23+
func ConvertToMap_Str_Op(array []map[string]Operations) map[string]Operations {
24+
result := make(map[string]Operations)
25+
for _, item := range array {
26+
for k, v := range item {
27+
result[k] = v
28+
}
29+
}
30+
return result
31+
}

0 commit comments

Comments
 (0)