Skip to content

Commit 9510b5a

Browse files
mirkoCrobumirkoCrobu
authored andcommitted
add brickInstance variables full info
1 parent 089698a commit 9510b5a

File tree

6 files changed

+65
-51
lines changed

6 files changed

+65
-51
lines changed

cmd/gendoc/docs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ Contains a JSON object with the details of an error.
909909
})(nil),
910910
CustomSuccessResponse: &CustomResponseDef{
911911
ContentType: "application/json",
912-
DataStructure: bricks.BrickInstance{},
912+
DataStructure: bricks.BrickInstanceDetails{},
913913
Description: "Successful response",
914914
StatusCode: http.StatusOK,
915915
},

internal/api/docs/openapi.yaml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ paths:
159159
content:
160160
application/json:
161161
schema:
162-
$ref: '#/components/schemas/BrickInstance'
162+
$ref: '#/components/schemas/BrickInstanceDetails'
163163
description: Successful response
164164
"400":
165165
$ref: '#/components/responses/BadRequest'
@@ -1183,7 +1183,7 @@ components:
11831183
properties:
11841184
bricks:
11851185
items:
1186-
$ref: '#/components/schemas/BrickInstance'
1186+
$ref: '#/components/schemas/BrickInstanceDetails'
11871187
nullable: true
11881188
type: array
11891189
type: object
@@ -1319,7 +1319,7 @@ components:
13191319
$ref: '#/components/schemas/BrickVariable'
13201320
type: object
13211321
type: object
1322-
BrickInstance:
1322+
BrickInstanceDetails:
13231323
properties:
13241324
author:
13251325
type: string
@@ -1335,9 +1335,18 @@ components:
13351335
type: string
13361336
variables:
13371337
additionalProperties:
1338-
type: string
1338+
$ref: '#/components/schemas/BrickInstanceVariable'
13391339
type: object
13401340
type: object
1341+
BrickInstanceVariable:
1342+
properties:
1343+
description:
1344+
type: string
1345+
required:
1346+
type: boolean
1347+
value:
1348+
type: string
1349+
type: object
13411350
BrickListItem:
13421351
properties:
13431352
author:

internal/e2e/client/client.gen.go

Lines changed: 19 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/e2e/daemon/instance_bricks_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ func TestUpsertAppBrickInstance(t *testing.T) {
162162
require.NoError(t, err)
163163
require.NotEmpty(t, brickInstance.JSON200)
164164
require.Equal(t, ImageClassifactionBrickID, *brickInstance.JSON200.Id)
165-
require.Equal(t, "/home/arduino/.arduino-bricks/ei-models", (*brickInstance.JSON200.Variables)["CUSTOM_MODEL_PATH"])
165+
expectedVariable := client.BrickInstanceVariable{
166+
Value: f.Ptr("/home/arduino/.arduino-bricks/ei-models"),
167+
Description: f.Ptr("path to the custom model directory"),
168+
Required: f.Ptr(false),
169+
}
170+
require.Equal(t, expectedVariable, (*brickInstance.JSON200.Variables)["CUSTOM_MODEL_PATH"])
166171
require.Equal(t, "mobilenet-image-classification", *brickInstance.JSON200.Model)
167172

168173
t.Run("OverrideBrickInstance", func(t *testing.T) {
@@ -311,7 +316,12 @@ func TestUpdateAppBrickInstance(t *testing.T) {
311316
require.NoError(t, err)
312317
require.NotEmpty(t, brickInstance.JSON200)
313318
require.Equal(t, ImageClassifactionBrickID, *brickInstance.JSON200.Id)
314-
require.Equal(t, "overidden", (*brickInstance.JSON200.Variables)["CUSTOM_MODEL_PATH"])
319+
expectedVariable := client.BrickInstanceVariable{
320+
Value: f.Ptr("overidden"),
321+
Description: f.Ptr("path to the custom model directory"),
322+
Required: f.Ptr(false),
323+
}
324+
require.Equal(t, expectedVariable, (*brickInstance.JSON200.Variables)["CUSTOM_MODEL_PATH"])
315325
require.Equal(t, "person-classification", *brickInstance.JSON200.Model)
316326
})
317327
t.Run("UpdateOnlyModel", func(t *testing.T) {

internal/orchestrator/bricks/bricks.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,36 +71,36 @@ func (s *Service) List() (BrickListResult, error) {
7171
}
7272

7373
func (s *Service) AppBrickInstancesList(a *app.ArduinoApp) (AppBrickInstancesResult, error) {
74-
res := AppBrickInstancesResult{BrickInstances: make([]BrickInstance, len(a.Descriptor.Bricks))}
74+
res := AppBrickInstancesResult{BrickInstances: make([]BrickInstanceDetails, len(a.Descriptor.Bricks))}
75+
7576
for i, brickInstance := range a.Descriptor.Bricks {
7677
brick, found := s.bricksIndex.FindBrickByID(brickInstance.ID)
7778
if !found {
7879
return AppBrickInstancesResult{}, fmt.Errorf("brick not found with id %s", brickInstance.ID)
7980
}
80-
res.BrickInstances[i] = BrickInstance{
81-
ID: brick.ID,
82-
Name: brick.Name,
83-
Author: "Arduino", // TODO: for now we only support our bricks
84-
Category: brick.Category,
85-
Status: "installed",
86-
ModelID: brickInstance.Model, // TODO: in case is not set by the user, should we return the default model?
87-
Variables: brickInstance.Variables, // TODO: do we want to show also the default value of not explicitly set variables?
88-
}
81+
res.BrickInstances[i] = s.buildBrickInstanceDetails(brick, brickInstance)
8982
}
83+
9084
return res, nil
9185
}
92-
9386
func (s *Service) AppBrickInstanceDetails(a *app.ArduinoApp, brickID string) (BrickInstanceDetails, error) {
87+
9488
brick, found := s.bricksIndex.FindBrickByID(brickID)
9589
if !found {
9690
return BrickInstanceDetails{}, ErrBrickNotFound
9791
}
98-
// Check if the brick is already added in the app
92+
9993
brickIndex := slices.IndexFunc(a.Descriptor.Bricks, func(b app.Brick) bool { return b.ID == brickID })
10094
if brickIndex == -1 {
10195
return BrickInstanceDetails{}, fmt.Errorf("brick %s not added in the app", brickID)
10296
}
97+
brickInstance := a.Descriptor.Bricks[brickIndex]
98+
details := s.buildBrickInstanceDetails(brick, brickInstance)
10399

100+
return details, nil
101+
}
102+
103+
func (s *Service) buildBrickInstanceDetails(brick *bricksindex.Brick, brickInstance app.Brick) BrickInstanceDetails {
104104
variables := make(map[string]BrickInstanceVariable, len(brick.Variables))
105105
for _, v := range brick.Variables {
106106
variables[v.Name] = BrickInstanceVariable{
@@ -109,9 +109,7 @@ func (s *Service) AppBrickInstanceDetails(a *app.ArduinoApp, brickID string) (Br
109109
Required: v.IsRequired(),
110110
}
111111
}
112-
// Add/Update the variables with the ones from the app descriptor
113-
appVars := a.Descriptor.Bricks[brickIndex].Variables
114-
for varName, appValue := range appVars {
112+
for varName, appValue := range brickInstance.Variables {
115113
if v, ok := variables[varName]; ok {
116114
v.Value = appValue
117115
variables[varName] = v
@@ -121,20 +119,20 @@ func (s *Service) AppBrickInstanceDetails(a *app.ArduinoApp, brickID string) (Br
121119
}
122120
}
123121
}
124-
modelID := a.Descriptor.Bricks[brickIndex].Model
122+
123+
modelID := brickInstance.Model
125124
if modelID == "" {
126125
modelID = brick.ModelName
127126
}
128-
129127
return BrickInstanceDetails{
130-
ID: brickID,
128+
ID: brick.ID,
131129
Name: brick.Name,
132-
Author: "Arduino", // TODO: for now we only support our bricks
130+
Author: "Arduino",
133131
Category: brick.Category,
134-
Status: "installed", // For now every Arduino brick are installed
132+
Status: "installed",
135133
Variables: variables,
136134
ModelID: modelID,
137-
}, nil
135+
}
138136
}
139137

140138
func (s *Service) BricksDetails(id string) (BrickDetailsResult, error) {

internal/orchestrator/bricks/types.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,7 @@ type BrickListItem struct {
3030
}
3131

3232
type AppBrickInstancesResult struct {
33-
BrickInstances []BrickInstance `json:"bricks"`
34-
}
35-
36-
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"`
33+
BrickInstances []BrickInstanceDetails `json:"bricks"`
4434
}
4535

4636
type BrickInstanceDetails struct {

0 commit comments

Comments
 (0)