2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2023-02-01 19:42:32 +00:00
|
|
|
package pbcommon
|
2020-09-18 19:36:12 +00:00
|
|
|
|
|
|
|
import (
|
2023-02-01 19:42:32 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2020-09-18 19:36:12 +00:00
|
|
|
)
|
|
|
|
|
2023-01-11 14:39:10 +00:00
|
|
|
// ProtobufTypesStructToMapStringInterface converts a protobuf/structpb.Struct into a
|
2020-09-18 19:36:12 +00:00
|
|
|
// map[string]interface{}.
|
2023-01-11 14:39:10 +00:00
|
|
|
func ProtobufTypesStructToMapStringInterface(s *structpb.Struct) map[string]interface{} {
|
2020-09-18 19:36:12 +00:00
|
|
|
if s == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-11 14:39:10 +00:00
|
|
|
return s.AsMap()
|
2020-09-18 19:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MapStringInterfaceToProtobufTypesStruct converts a map[string]interface{} into a proto.Struct
|
2023-01-11 14:39:10 +00:00
|
|
|
func MapStringInterfaceToProtobufTypesStruct(m map[string]interface{}) *structpb.Struct {
|
|
|
|
if len(m) < 1 {
|
2020-09-18 19:36:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-11 14:39:10 +00:00
|
|
|
// TODO - handle the error better. It probably requires mog to be able to use alternative method signatures though
|
|
|
|
s, _ := structpb.NewStruct(m)
|
|
|
|
return s
|
2020-09-18 19:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SliceToPBListValue converts a []interface{} into a proto.ListValue. It's used
|
|
|
|
// internally by MapStringInterfaceToProtobufTypesStruct when it encouters slices.
|
2023-01-11 14:39:10 +00:00
|
|
|
// TODO (remove usage of this struct in favor of structpb.NewListValue)
|
|
|
|
func SliceToPBListValue(s []interface{}) *structpb.ListValue {
|
|
|
|
if len(s) < 1 {
|
2020-09-18 19:36:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-11 14:39:10 +00:00
|
|
|
// TODO - handle the error better. It probably requires mog to use alt method signatures though
|
|
|
|
val, _ := structpb.NewList(s)
|
|
|
|
return val
|
2020-09-18 19:36:12 +00:00
|
|
|
}
|
2023-02-01 19:42:32 +00:00
|
|
|
|
|
|
|
// EnvoyExtensionsToStructs takes a protobuf EnvoyExtension argument and converts it to the
|
|
|
|
// structs EnvoyExtension
|
|
|
|
func EnvoyExtensionsToStructs(args []*EnvoyExtension) []structs.EnvoyExtension {
|
|
|
|
o := make([]structs.EnvoyExtension, len(args))
|
|
|
|
for i := range args {
|
|
|
|
var e structs.EnvoyExtension
|
|
|
|
if args[i] != nil {
|
|
|
|
e = structs.EnvoyExtension{
|
2023-06-08 14:26:11 +00:00
|
|
|
Name: args[i].Name,
|
|
|
|
Required: args[i].Required,
|
|
|
|
ConsulVersion: args[i].ConsulVersion,
|
|
|
|
EnvoyVersion: args[i].EnvoyVersion,
|
|
|
|
Arguments: ProtobufTypesStructToMapStringInterface(args[i].Arguments),
|
2023-02-01 19:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
o[i] = e
|
|
|
|
}
|
|
|
|
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnvoyExtensionsFromStructs takes a structs EnvoyExtension argument and converts it to the
|
|
|
|
// protobuf EnvoyExtension
|
|
|
|
func EnvoyExtensionsFromStructs(args []structs.EnvoyExtension) []*EnvoyExtension {
|
|
|
|
o := make([]*EnvoyExtension, len(args))
|
|
|
|
for i, e := range args {
|
|
|
|
o[i] = &EnvoyExtension{
|
2023-06-08 14:26:11 +00:00
|
|
|
Name: e.Name,
|
|
|
|
Required: e.Required,
|
|
|
|
ConsulVersion: e.ConsulVersion,
|
|
|
|
EnvoyVersion: e.EnvoyVersion,
|
|
|
|
Arguments: MapStringInterfaceToProtobufTypesStruct(e.Arguments),
|
2023-02-01 19:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return o
|
|
|
|
}
|