Skip to content

Commit 87d8f44

Browse files
mirkoCrobumirkoCrobu
authored andcommitted
add variables_details
1 parent f61b73f commit 87d8f44

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

internal/orchestrator/bricks/bricks.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,20 @@ func (s *Service) AppBrickInstancesList(a *app.ArduinoApp) (AppBrickInstancesRes
7878
if !found {
7979
return AppBrickInstancesResult{}, fmt.Errorf("brick not found with id %s", brickInstance.ID)
8080
}
81+
82+
instanceVariables := getBrickInstanceVariableDetails(brick, brickInstance.Variables)
83+
8184
res.BrickInstances[i] = BrickInstance{
82-
ID: brick.ID,
83-
Name: brick.Name,
84-
Author: "Arduino", // TODO: for now we only support our bricks
85-
Category: brick.Category,
86-
Status: "installed",
87-
ModelID: brickInstance.Model, // TODO: in case is not set by the user, should we return the default model?
88-
Variables: brickInstance.Variables, // TODO: do we want to show also the default value of not explicitly set variables?
85+
ID: brick.ID,
86+
Name: brick.Name,
87+
Author: "Arduino", // TODO: for now we only support our bricks
88+
Category: brick.Category,
89+
Status: "installed",
90+
ModelID: brickInstance.Model, // TODO: in case is not set by the user, should we return the default model?
91+
Variables: brickInstance.Variables, // TODO: do we want to show also the default value of not explicitly set variables?
92+
VariablesDetails: instanceVariables,
8993
}
94+
9095
}
9196
return res, nil
9297
}
@@ -109,21 +114,40 @@ func (s *Service) AppBrickInstanceDetails(a *app.ArduinoApp, brickID string) (Br
109114
// Add/Update the variables with the ones from the app descriptor
110115
maps.Copy(variables, a.Descriptor.Bricks[brickIndex].Variables)
111116

117+
instanceVariables := getBrickInstanceVariableDetails(brick, a.Descriptor.Bricks[brickIndex].Variables)
118+
112119
modelID := a.Descriptor.Bricks[brickIndex].Model
113120
if modelID == "" {
114121
modelID = brick.ModelName
115122
}
116123

117124
return BrickInstance{
118-
ID: brickID,
119-
Name: brick.Name,
120-
Author: "Arduino", // TODO: for now we only support our bricks
121-
Category: brick.Category,
122-
Status: "installed", // For now every Arduino brick are installed
123-
Variables: variables,
124-
ModelID: modelID,
125+
ID: brickID,
126+
Name: brick.Name,
127+
Author: "Arduino", // TODO: for now we only support our bricks
128+
Category: brick.Category,
129+
Status: "installed", // For now every Arduino brick are installed
130+
Variables: variables,
131+
VariablesDetails: instanceVariables,
132+
ModelID: modelID,
125133
}, nil
126134
}
135+
func getBrickInstanceVariableDetails(
136+
brick *bricksindex.Brick,
137+
brickInstanceVariables map[string]string,
138+
) []BrickInstanceVariable {
139+
variableDetails := make([]BrickInstanceVariable, 0, len(brick.Variables))
140+
for _, v := range brick.Variables {
141+
value := brickInstanceVariables[v.Name]
142+
variableDetails = append(variableDetails, BrickInstanceVariable{
143+
Name: v.Name,
144+
Value: value,
145+
Description: v.Description,
146+
Required: v.IsRequired(),
147+
})
148+
}
149+
return variableDetails
150+
}
127151

128152
func (s *Service) BricksDetails(id string) (BrickDetailsResult, error) {
129153
brick, found := s.bricksIndex.FindBrickByID(id)

internal/orchestrator/bricks/types.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,21 @@ type AppBrickInstancesResult struct {
3434
}
3535

3636
type BrickInstance struct {
37-
ID string `json:"id"`
38-
Name string `json:"name"`
39-
Author string `json:"author"`
40-
Category string `json:"category"`
41-
Status string `json:"status"`
42-
Variables map[string]string `json:"variables,omitempty"`
43-
ModelID string `json:"model,omitempty"`
37+
ID string `json:"id"`
38+
Name string `json:"name"`
39+
Author string `json:"author"`
40+
Category string `json:"category"`
41+
Status string `json:"status"`
42+
Variables map[string]string `json:"variables,omitempty"`
43+
VariablesDetails []BrickInstanceVariable `json:"variables_details,omitempty"`
44+
ModelID string `json:"model,omitempty"`
45+
}
46+
47+
type BrickInstanceVariable struct {
48+
Name string `json:"name"`
49+
Value string `json:"value"`
50+
Description string `json:"description"`
51+
Required bool `json:"required"`
4452
}
4553

4654
type BrickVariable struct {

0 commit comments

Comments
 (0)