-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v3' into key-via-file-path
- Loading branch information
Showing
32 changed files
with
3,335 additions
and
606 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) F5, Inc. | ||
// | ||
// This source code is licensed under the Apache License, Version 2.0 license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
package v1 | ||
|
||
import ( | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
// ConvertToStructs converts a map[string]any into a slice of *structpb.Struct. | ||
// Each key-value pair in the input map is converted into a *structpb.Struct, | ||
// where the key is used as the field name, and the value is added to the Struct. | ||
// | ||
// Parameters: | ||
// - input: A map[string]any containing key-value pairs to be converted. | ||
// | ||
// Returns: | ||
// - []*structpb.Struct: A slice of *structpb.Struct, where each map entry is converted into a struct. | ||
// - error: An error if any value in the input map cannot be converted into a *structpb.Struct. | ||
// | ||
// Example: | ||
// | ||
// input := map[string]any{ | ||
// "key1": "value1", | ||
// "key2": 123, | ||
// "key3": true, | ||
// } | ||
// structs, err := ConvertToStructs(input) | ||
// // structs will contain a slice of *structpb.Struct | ||
// // err will be nil if all conversions succeed. | ||
func ConvertToStructs(input map[string]any) ([]*structpb.Struct, error) { | ||
structs := []*structpb.Struct{} | ||
for key, value := range input { | ||
// Convert each value in the map to *structpb.Struct | ||
structValue, err := structpb.NewStruct(map[string]any{ | ||
key: value, | ||
}) | ||
if err != nil { | ||
return structs, err | ||
} | ||
structs = append(structs, structValue) | ||
} | ||
|
||
return structs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) F5, Inc. | ||
// | ||
// This source code is licensed under the Apache License, Version 2.0 license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
func TestConvertToStructs(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input map[string]any | ||
expected []*structpb.Struct | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Test 1: Valid input with simple key-value pairs", | ||
input: map[string]any{ | ||
"key1": "value1", | ||
"key2": 123, | ||
"key3": true, | ||
}, | ||
expected: []*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), | ||
}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test 2: Empty input map", | ||
input: make(map[string]any), | ||
expected: []*structpb.Struct{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Test 3: Invalid input type", | ||
input: map[string]any{ | ||
"key1": func() {}, // Unsupported type | ||
}, | ||
expected: []*structpb.Struct{}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ConvertToStructs(tt.input) | ||
|
||
assert.Equal(t, tt.expected, got) | ||
assert.Equal(t, tt.wantErr, err != nil) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.