Skip to content

Commit bf8a15f

Browse files
mirkoCrobulucarin91
authored andcommitted
add variable info to instaceBrick details
1 parent f61b73f commit bf8a15f

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

internal/orchestrator/bricks/bricks.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package bricks
1818
import (
1919
"errors"
2020
"fmt"
21-
"maps"
2221
"slices"
2322

2423
"github.com/arduino/go-paths-helper"
@@ -91,30 +90,43 @@ func (s *Service) AppBrickInstancesList(a *app.ArduinoApp) (AppBrickInstancesRes
9190
return res, nil
9291
}
9392

94-
func (s *Service) AppBrickInstanceDetails(a *app.ArduinoApp, brickID string) (BrickInstance, error) {
93+
func (s *Service) AppBrickInstanceDetails(a *app.ArduinoApp, brickID string) (BrickInstanceDetails, error) {
9594
brick, found := s.bricksIndex.FindBrickByID(brickID)
9695
if !found {
97-
return BrickInstance{}, ErrBrickNotFound
96+
return BrickInstanceDetails{}, ErrBrickNotFound
9897
}
9998
// Check if the brick is already added in the app
10099
brickIndex := slices.IndexFunc(a.Descriptor.Bricks, func(b app.Brick) bool { return b.ID == brickID })
101100
if brickIndex == -1 {
102-
return BrickInstance{}, fmt.Errorf("brick %s not added in the app", brickID)
101+
return BrickInstanceDetails{}, fmt.Errorf("brick %s not added in the app", brickID)
103102
}
104103

105-
variables := make(map[string]string, len(brick.Variables))
104+
variables := make(map[string]BrickVariable, len(brick.Variables))
106105
for _, v := range brick.Variables {
107-
variables[v.Name] = v.DefaultValue
106+
variables[v.Name] = BrickVariable{
107+
DefaultValue: v.DefaultValue,
108+
Description: v.Description,
109+
Required: v.IsRequired(),
110+
}
108111
}
109112
// Add/Update the variables with the ones from the app descriptor
110-
maps.Copy(variables, a.Descriptor.Bricks[brickIndex].Variables)
111-
113+
appVars := a.Descriptor.Bricks[brickIndex].Variables
114+
for varName, appValue := range appVars {
115+
if v, ok := variables[varName]; ok {
116+
v.DefaultValue = appValue
117+
variables[varName] = v
118+
} else {
119+
variables[varName] = BrickVariable{
120+
DefaultValue: appValue,
121+
}
122+
}
123+
}
112124
modelID := a.Descriptor.Bricks[brickIndex].Model
113125
if modelID == "" {
114126
modelID = brick.ModelName
115127
}
116128

117-
return BrickInstance{
129+
return BrickInstanceDetails{
118130
ID: brickID,
119131
Name: brick.Name,
120132
Author: "Arduino", // TODO: for now we only support our bricks

internal/orchestrator/bricks/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ type BrickInstance struct {
4343
ModelID string `json:"model,omitempty"`
4444
}
4545

46+
type BrickInstanceDetails struct {
47+
ID string `json:"id"`
48+
Name string `json:"name"`
49+
Author string `json:"author"`
50+
Category string `json:"category"`
51+
Status string `json:"status"`
52+
Variables map[string]BrickVariable `json:"variables,omitempty"`
53+
ModelID string `json:"model,omitempty"`
54+
}
55+
4656
type BrickVariable struct {
4757
DefaultValue string `json:"default_value,omitempty"`
4858
Description string `json:"description,omitempty"`

0 commit comments

Comments
 (0)