Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions api/grpc/mpi/v1/command_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions api/grpc/mpi/v1/files_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions api/grpc/mpi/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package v1

import (
"log/slog"

"google.golang.org/protobuf/types/known/structpb"
)

Expand Down Expand Up @@ -45,3 +47,28 @@ func ConvertToStructs(input map[string]any) ([]*structpb.Struct, error) {

return structs, nil
}

func ConvertToMap(input []*structpb.Struct) map[string]any {
convertedMap := make(map[string]any)
for _, value := range input {
for key, field := range value.GetFields() {
kind := field.GetKind()
switch kind.(type) {
case *structpb.Value_StringValue:
convertedMap[key] = field.GetStringValue()
case *structpb.Value_NumberValue:
convertedMap[key] = int(field.GetNumberValue())
case *structpb.Value_StructValue:
convertedMap[key] = field.GetStructValue()
case *structpb.Value_ListValue:
convertedMap[key] = field.GetListValue()
case *structpb.Value_BoolValue:
convertedMap[key] = field.GetBoolValue()
default:
slog.Warn("unknown type for map conversion", "value", kind)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
slog.Warn("unknown type for map conversion", "value", kind)
slog.Warn("Unknown type for map conversion", "key", key, "value", kind)

}
}
}

return convertedMap
}
82 changes: 82 additions & 0 deletions api/grpc/mpi/v1/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,85 @@ func TestConvertToStructs(t *testing.T) {
})
}
}

func TestConvertToMaps(t *testing.T) {
tests := []struct {
name string
expected map[string]any
input []*structpb.Struct
}{
{
name: "Test 1: Valid input with simple key-value pairs",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing cases for slices, and structs

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a test for invalid values? Empty values, blank "" values and special characters.

For valid scenario, should we include "config-sync-group" key?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests for this function should be pretty generic, the exact strings we place in the tests shouldn't really matter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only to covert to a map validation will be done elsewhere

expected: map[string]any{
"key1": "value1",
"key2": 123,
"key3": true,
},
input: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
"key1": structpb.NewStringValue("value1"),
},
},
{
Fields: map[string]*structpb.Value{
"key2": structpb.NewNumberValue(123),
},
},
{
Fields: map[string]*structpb.Value{
"key3": structpb.NewBoolValue(true),
},
},
},
},
{
name: "Test 2: Valid input with simple key-value pairs, struct and list ",
expected: map[string]any{
"key1": "value1",
"key2": &structpb.ListValue{Values: []*structpb.Value{
structpb.NewStringValue("value1"),
structpb.NewStringValue("value2"),
}},
"key3": &structpb.Struct{
Fields: map[string]*structpb.Value{
"key1": structpb.NewStringValue("value1"),
},
},
},
input: []*structpb.Struct{
{
Fields: map[string]*structpb.Value{
"key1": structpb.NewStringValue("value1"),
},
},
{
Fields: map[string]*structpb.Value{
"key2": structpb.NewListValue(&structpb.ListValue{
Values: []*structpb.Value{
structpb.NewStringValue("value1"),
structpb.NewStringValue("value2"),
},
}),
},
},

{
Fields: map[string]*structpb.Value{
"key3": structpb.NewStructValue(&structpb.Struct{
Fields: map[string]*structpb.Value{"key1": structpb.NewStringValue("value1")},
}),
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ConvertToMap(tt.input)

assert.Equal(t, tt.expected, got)
})
}
}
2 changes: 1 addition & 1 deletion internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (a *App) Run(ctx context.Context) error {
slog.String("commit", a.commit),
)

messagePipe := bus.NewMessagePipe(defaultMessagePipeChannelSize)
messagePipe := bus.NewMessagePipe(defaultMessagePipeChannelSize, agentConfig)
err = messagePipe.Register(defaultQueueSize, plugin.LoadPlugins(ctx, agentConfig))
if err != nil {
slog.ErrorContext(ctx, "Failed to register plugins", "error", err)
Expand Down
Loading
Loading