proto: add code to convert between protobuf types and structs types

For streaming service health events.

Code generated using mog.
This commit is contained in:
Daniel Nephin 2020-09-18 14:05:15 -04:00
parent b4c48d4417
commit b6e253e352
7 changed files with 616 additions and 4 deletions

242
proto/pbservice/convert.go Normal file
View File

@ -0,0 +1,242 @@
package pbservice
import (
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/proto/pbcommon"
)
func RaftIndexToStructs(s pbcommon.RaftIndex) structs.RaftIndex {
return structs.RaftIndex{
CreateIndex: s.CreateIndex,
ModifyIndex: s.ModifyIndex,
}
}
func NewRaftIndexFromStructs(s structs.RaftIndex) pbcommon.RaftIndex {
return pbcommon.RaftIndex{
CreateIndex: s.CreateIndex,
ModifyIndex: s.ModifyIndex,
}
}
func MapHeadersToStructs(s map[string]HeaderValue) map[string][]string {
t := make(map[string][]string, len(s))
for k, v := range s {
t[k] = v.Value
}
return t
}
func NewMapHeadersFromStructs(t map[string][]string) map[string]HeaderValue {
s := make(map[string]HeaderValue, len(t))
for k, v := range t {
s[k] = HeaderValue{Value: v}
}
return s
}
// TODO: use mog once it supports pointers and slices
func CheckServiceNodeToStructs(s *CheckServiceNode) *structs.CheckServiceNode {
if s == nil {
return nil
}
var t structs.CheckServiceNode
if s.Node != nil {
n := NodeToStructs(*s.Node)
t.Node = &n
}
if s.Service != nil {
r := NodeServiceToStructs(*s.Service)
t.Service = &r
}
t.Checks = make(structs.HealthChecks, len(s.Checks))
for i, c := range s.Checks {
if c == nil {
continue
}
h := HealthCheckToStructs(*c)
t.Checks[i] = &h
}
return &t
}
// TODO: use mog once it supports pointers and slices
func NewCheckServiceNodeFromStructs(t *structs.CheckServiceNode) *CheckServiceNode {
if t == nil {
return nil
}
var s CheckServiceNode
if t.Node != nil {
n := NewNodeFromStructs(*t.Node)
s.Node = &n
}
if t.Service != nil {
r := NewNodeServiceFromStructs(*t.Service)
s.Service = &r
}
t.Checks = make(structs.HealthChecks, len(t.Checks))
for i, c := range t.Checks {
if c == nil {
continue
}
h := NewHealthCheckFromStructs(*c)
s.Checks[i] = &h
}
return &s
}
// TODO: handle this with mog, once mog handles pointers
func WeightsPtrToStructs(s *Weights) *structs.Weights {
if s == nil {
return nil
}
var t structs.Weights
t.Passing = int(s.Passing)
t.Warning = int(s.Warning)
return &t
}
// TODO: handle this with mog, once mog handles pointers
func NewWeightsPtrFromStructs(t *structs.Weights) *Weights {
if t == nil {
return nil
}
var s Weights
s.Passing = int32(t.Passing)
s.Warning = int32(t.Warning)
return &s
}
// TODO: handle this with mog
func MapStringServiceAddressToStructs(s map[string]ServiceAddress) map[string]structs.ServiceAddress {
t := make(map[string]structs.ServiceAddress, len(s))
for k, v := range s {
t[k] = structs.ServiceAddress{Address: v.Address, Port: int(v.Port)}
}
return t
}
// TODO: handle this with mog
func NewMapStringServiceAddressFromStructs(t map[string]structs.ServiceAddress) map[string]ServiceAddress {
s := make(map[string]ServiceAddress, len(t))
for k, v := range t {
s[k] = ServiceAddress{Address: v.Address, Port: int32(v.Port)}
}
return s
}
// TODO: handle this with mog
func ExposePathSliceToStructs(s []ExposePath) []structs.ExposePath {
t := make([]structs.ExposePath, len(s))
for i, v := range s {
t[i] = ExposePathToStructs(v)
}
return t
}
// TODO: handle this with mog
func NewExposePathSliceFromStructs(t []structs.ExposePath) []ExposePath {
s := make([]ExposePath, len(t))
for i, v := range t {
s[i] = NewExposePathFromStructs(v)
}
return s
}
// TODO: handle this with mog
func UpstreamsToStructs(s []Upstream) structs.Upstreams {
t := make(structs.Upstreams, len(s))
for i, v := range s {
t[i] = UpstreamToStructs(v)
}
return t
}
// TODO: handle this with mog
func NewUpstreamsFromStructs(t structs.Upstreams) []Upstream {
s := make([]Upstream, len(t))
for i, v := range t {
s[i] = NewUpstreamFromStructs(v)
}
return s
}
// TODO: handle this with mog
func CheckTypesToStructs(s []*CheckType) structs.CheckTypes {
t := make(structs.CheckTypes, len(s))
for i, v := range s {
if v == nil {
continue
}
newV := CheckTypeToStructs(*v)
t[i] = &newV
}
return t
}
// TODO: handle this with mog
func NewCheckTypesFromStructs(t structs.CheckTypes) []*CheckType {
s := make([]*CheckType, len(t))
for i, v := range t {
if v == nil {
continue
}
newV := NewCheckTypeFromStructs(*v)
s[i] = &newV
}
return s
}
// TODO: handle this with mog
func ConnectProxyConfigPtrToStructs(s *ConnectProxyConfig) *structs.ConnectProxyConfig {
if s == nil {
return nil
}
t := ConnectProxyConfigToStructs(*s)
return &t
}
// TODO: handle this with mog
func NewConnectProxyConfigPtrFromStructs(t *structs.ConnectProxyConfig) *ConnectProxyConfig {
if t == nil {
return nil
}
s := NewConnectProxyConfigFromStructs(*t)
return &s
}
// TODO: handle this with mog
func ServiceConnectPtrToStructs(s *ServiceConnect) *structs.ServiceConnect {
if s == nil {
return nil
}
t := ServiceConnectToStructs(*s)
return &t
}
// TODO: handle this with mog
func NewServiceConnectPtrFromStructs(t *structs.ServiceConnect) *ServiceConnect {
if t == nil {
return nil
}
s := NewServiceConnectFromStructs(*t)
return &s
}
// TODO: handle this with mog
func ServiceDefinitionPtrToStructs(s *ServiceDefinition) *structs.ServiceDefinition {
if s == nil {
return nil
}
t := ServiceDefinitionToStructs(*s)
return &t
}
// TODO: handle this with mog
func NewServiceDefinitionPtrFromStructs(t *structs.ServiceDefinition) *ServiceDefinition {
if t == nil {
return nil
}
s := NewServiceDefinitionFromStructs(*t)
return &s
}

View File

@ -0,0 +1,14 @@
package pbservice
import (
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/proto/pbcommon"
)
func EnterpriseMetaToStructs(_ pbcommon.EnterpriseMeta) structs.EnterpriseMeta {
return structs.EnterpriseMeta{}
}
func NewEnterpriseMetaFromStructs(_ structs.EnterpriseMeta) pbcommon.EnterpriseMeta {
return pbcommon.EnterpriseMeta{}
}

View File

@ -0,0 +1,144 @@
// Code generated by mog. DO NOT EDIT.
package pbservice
import structs "github.com/hashicorp/consul/agent/structs"
func CheckTypeToStructs(s CheckType) structs.CheckType {
var t structs.CheckType
t.CheckID = s.CheckID
t.Name = s.Name
t.Status = s.Status
t.Notes = s.Notes
t.ScriptArgs = s.ScriptArgs
t.HTTP = s.HTTP
t.Header = MapHeadersToStructs(s.Header)
t.Method = s.Method
t.Body = s.Body
t.TCP = s.TCP
t.Interval = s.Interval
t.AliasNode = s.AliasNode
t.AliasService = s.AliasService
t.DockerContainerID = s.DockerContainerID
t.Shell = s.Shell
t.GRPC = s.GRPC
t.GRPCUseTLS = s.GRPCUseTLS
t.TLSSkipVerify = s.TLSSkipVerify
t.Timeout = s.Timeout
t.TTL = s.TTL
t.SuccessBeforePassing = int(s.SuccessBeforePassing)
t.FailuresBeforeCritical = int(s.FailuresBeforeCritical)
t.ProxyHTTP = s.ProxyHTTP
t.ProxyGRPC = s.ProxyGRPC
t.DeregisterCriticalServiceAfter = s.DeregisterCriticalServiceAfter
t.OutputMaxSize = int(s.OutputMaxSize)
return t
}
func NewCheckTypeFromStructs(t structs.CheckType) CheckType {
var s CheckType
s.CheckID = t.CheckID
s.Name = t.Name
s.Status = t.Status
s.Notes = t.Notes
s.ScriptArgs = t.ScriptArgs
s.HTTP = t.HTTP
s.Header = NewMapHeadersFromStructs(t.Header)
s.Method = t.Method
s.Body = t.Body
s.TCP = t.TCP
s.Interval = t.Interval
s.AliasNode = t.AliasNode
s.AliasService = t.AliasService
s.DockerContainerID = t.DockerContainerID
s.Shell = t.Shell
s.GRPC = t.GRPC
s.GRPCUseTLS = t.GRPCUseTLS
s.TLSSkipVerify = t.TLSSkipVerify
s.Timeout = t.Timeout
s.TTL = t.TTL
s.SuccessBeforePassing = int32(t.SuccessBeforePassing)
s.FailuresBeforeCritical = int32(t.FailuresBeforeCritical)
s.ProxyHTTP = t.ProxyHTTP
s.ProxyGRPC = t.ProxyGRPC
s.DeregisterCriticalServiceAfter = t.DeregisterCriticalServiceAfter
s.OutputMaxSize = int32(t.OutputMaxSize)
return s
}
func HealthCheckToStructs(s HealthCheck) structs.HealthCheck {
var t structs.HealthCheck
t.Node = s.Node
t.CheckID = s.CheckID
t.Name = s.Name
t.Status = s.Status
t.Notes = s.Notes
t.Output = s.Output
t.ServiceID = s.ServiceID
t.ServiceName = s.ServiceName
t.ServiceTags = s.ServiceTags
t.Type = s.Type
t.Definition = HealthCheckDefinitionToStructs(s.Definition)
t.EnterpriseMeta = EnterpriseMetaToStructs(s.EnterpriseMeta)
t.RaftIndex = RaftIndexToStructs(s.RaftIndex)
return t
}
func NewHealthCheckFromStructs(t structs.HealthCheck) HealthCheck {
var s HealthCheck
s.Node = t.Node
s.CheckID = t.CheckID
s.Name = t.Name
s.Status = t.Status
s.Notes = t.Notes
s.Output = t.Output
s.ServiceID = t.ServiceID
s.ServiceName = t.ServiceName
s.ServiceTags = t.ServiceTags
s.Type = t.Type
s.Definition = NewHealthCheckDefinitionFromStructs(t.Definition)
s.EnterpriseMeta = NewEnterpriseMetaFromStructs(t.EnterpriseMeta)
s.RaftIndex = NewRaftIndexFromStructs(t.RaftIndex)
return s
}
func HealthCheckDefinitionToStructs(s HealthCheckDefinition) structs.HealthCheckDefinition {
var t structs.HealthCheckDefinition
t.HTTP = s.HTTP
t.TLSSkipVerify = s.TLSSkipVerify
t.Header = MapHeadersToStructs(s.Header)
t.Method = s.Method
t.Body = s.Body
t.TCP = s.TCP
t.Interval = s.Interval
t.OutputMaxSize = uint(s.OutputMaxSize)
t.Timeout = s.Timeout
t.DeregisterCriticalServiceAfter = s.DeregisterCriticalServiceAfter
t.ScriptArgs = s.ScriptArgs
t.DockerContainerID = s.DockerContainerID
t.Shell = s.Shell
t.GRPC = s.GRPC
t.GRPCUseTLS = s.GRPCUseTLS
t.AliasNode = s.AliasNode
t.AliasService = s.AliasService
t.TTL = s.TTL
return t
}
func NewHealthCheckDefinitionFromStructs(t structs.HealthCheckDefinition) HealthCheckDefinition {
var s HealthCheckDefinition
s.HTTP = t.HTTP
s.TLSSkipVerify = t.TLSSkipVerify
s.Header = NewMapHeadersFromStructs(t.Header)
s.Method = t.Method
s.Body = t.Body
s.TCP = t.TCP
s.Interval = t.Interval
s.OutputMaxSize = uint32(t.OutputMaxSize)
s.Timeout = t.Timeout
s.DeregisterCriticalServiceAfter = t.DeregisterCriticalServiceAfter
s.ScriptArgs = t.ScriptArgs
s.DockerContainerID = t.DockerContainerID
s.Shell = t.Shell
s.GRPC = t.GRPC
s.GRPCUseTLS = t.GRPCUseTLS
s.AliasNode = t.AliasNode
s.AliasService = t.AliasService
s.TTL = t.TTL
return s
}

View File

@ -0,0 +1,66 @@
// Code generated by mog. DO NOT EDIT.
package pbservice
import structs "github.com/hashicorp/consul/agent/structs"
func NodeToStructs(s Node) structs.Node {
var t structs.Node
t.ID = s.ID
t.Node = s.Node
t.Address = s.Address
t.Datacenter = s.Datacenter
t.TaggedAddresses = s.TaggedAddresses
t.Meta = s.Meta
t.RaftIndex = RaftIndexToStructs(s.RaftIndex)
return t
}
func NewNodeFromStructs(t structs.Node) Node {
var s Node
s.ID = t.ID
s.Node = t.Node
s.Address = t.Address
s.Datacenter = t.Datacenter
s.TaggedAddresses = t.TaggedAddresses
s.Meta = t.Meta
s.RaftIndex = NewRaftIndexFromStructs(t.RaftIndex)
return s
}
func NodeServiceToStructs(s NodeService) structs.NodeService {
var t structs.NodeService
t.Kind = s.Kind
t.ID = s.ID
t.Service = s.Service
t.Tags = s.Tags
t.Address = s.Address
t.TaggedAddresses = MapStringServiceAddressToStructs(s.TaggedAddresses)
t.Meta = s.Meta
t.Port = int(s.Port)
t.Weights = WeightsPtrToStructs(s.Weights)
t.EnableTagOverride = s.EnableTagOverride
t.Proxy = ConnectProxyConfigToStructs(s.Proxy)
t.Connect = ServiceConnectToStructs(s.Connect)
t.LocallyRegisteredAsSidecar = s.LocallyRegisteredAsSidecar
t.EnterpriseMeta = EnterpriseMetaToStructs(s.EnterpriseMeta)
t.RaftIndex = RaftIndexToStructs(s.RaftIndex)
return t
}
func NewNodeServiceFromStructs(t structs.NodeService) NodeService {
var s NodeService
s.Kind = t.Kind
s.ID = t.ID
s.Service = t.Service
s.Tags = t.Tags
s.Address = t.Address
s.TaggedAddresses = NewMapStringServiceAddressFromStructs(t.TaggedAddresses)
s.Meta = t.Meta
s.Port = int32(t.Port)
s.Weights = NewWeightsPtrFromStructs(t.Weights)
s.EnableTagOverride = t.EnableTagOverride
s.Proxy = NewConnectProxyConfigFromStructs(t.Proxy)
s.Connect = NewServiceConnectFromStructs(t.Connect)
s.LocallyRegisteredAsSidecar = t.LocallyRegisteredAsSidecar
s.EnterpriseMeta = NewEnterpriseMetaFromStructs(t.EnterpriseMeta)
s.RaftIndex = NewRaftIndexFromStructs(t.RaftIndex)
return s
}

View File

@ -0,0 +1,146 @@
// Code generated by mog. DO NOT EDIT.
package pbservice
import structs "github.com/hashicorp/consul/agent/structs"
func ConnectProxyConfigToStructs(s ConnectProxyConfig) structs.ConnectProxyConfig {
var t structs.ConnectProxyConfig
t.DestinationServiceName = s.DestinationServiceName
t.DestinationServiceID = s.DestinationServiceID
t.LocalServiceAddress = s.LocalServiceAddress
t.LocalServicePort = int(s.LocalServicePort)
t.Config = ProtobufTypesStructToMapStringInterface(s.Config)
t.Upstreams = UpstreamsToStructs(s.Upstreams)
t.MeshGateway = MeshGatewayConfigToStructs(s.MeshGateway)
t.Expose = ExposeConfigToStructs(s.Expose)
return t
}
func NewConnectProxyConfigFromStructs(t structs.ConnectProxyConfig) ConnectProxyConfig {
var s ConnectProxyConfig
s.DestinationServiceName = t.DestinationServiceName
s.DestinationServiceID = t.DestinationServiceID
s.LocalServiceAddress = t.LocalServiceAddress
s.LocalServicePort = int32(t.LocalServicePort)
s.Config = MapStringInterfaceToProtobufTypesStruct(t.Config)
s.Upstreams = NewUpstreamsFromStructs(t.Upstreams)
s.MeshGateway = NewMeshGatewayConfigFromStructs(t.MeshGateway)
s.Expose = NewExposeConfigFromStructs(t.Expose)
return s
}
func ExposeConfigToStructs(s ExposeConfig) structs.ExposeConfig {
var t structs.ExposeConfig
t.Checks = s.Checks
t.Paths = ExposePathSliceToStructs(s.Paths)
return t
}
func NewExposeConfigFromStructs(t structs.ExposeConfig) ExposeConfig {
var s ExposeConfig
s.Checks = t.Checks
s.Paths = NewExposePathSliceFromStructs(t.Paths)
return s
}
func ExposePathToStructs(s ExposePath) structs.ExposePath {
var t structs.ExposePath
t.ListenerPort = int(s.ListenerPort)
t.Path = s.Path
t.LocalPathPort = int(s.LocalPathPort)
t.Protocol = s.Protocol
t.ParsedFromCheck = s.ParsedFromCheck
return t
}
func NewExposePathFromStructs(t structs.ExposePath) ExposePath {
var s ExposePath
s.ListenerPort = int32(t.ListenerPort)
s.Path = t.Path
s.LocalPathPort = int32(t.LocalPathPort)
s.Protocol = t.Protocol
s.ParsedFromCheck = t.ParsedFromCheck
return s
}
func MeshGatewayConfigToStructs(s MeshGatewayConfig) structs.MeshGatewayConfig {
var t structs.MeshGatewayConfig
t.Mode = s.Mode
return t
}
func NewMeshGatewayConfigFromStructs(t structs.MeshGatewayConfig) MeshGatewayConfig {
var s MeshGatewayConfig
s.Mode = t.Mode
return s
}
func ServiceConnectToStructs(s ServiceConnect) structs.ServiceConnect {
var t structs.ServiceConnect
t.Native = s.Native
t.SidecarService = ServiceDefinitionPtrToStructs(s.SidecarService)
return t
}
func NewServiceConnectFromStructs(t structs.ServiceConnect) ServiceConnect {
var s ServiceConnect
s.Native = t.Native
s.SidecarService = NewServiceDefinitionPtrFromStructs(t.SidecarService)
return s
}
func ServiceDefinitionToStructs(s ServiceDefinition) structs.ServiceDefinition {
var t structs.ServiceDefinition
t.Kind = s.Kind
t.ID = s.ID
t.Name = s.Name
t.Tags = s.Tags
t.Address = s.Address
t.TaggedAddresses = MapStringServiceAddressToStructs(s.TaggedAddresses)
t.Meta = s.Meta
t.Port = int(s.Port)
t.Check = CheckTypeToStructs(s.Check)
t.Checks = CheckTypesToStructs(s.Checks)
t.Weights = WeightsPtrToStructs(s.Weights)
t.Token = s.Token
t.EnableTagOverride = s.EnableTagOverride
t.Proxy = ConnectProxyConfigPtrToStructs(s.Proxy)
t.EnterpriseMeta = EnterpriseMetaToStructs(s.EnterpriseMeta)
t.Connect = ServiceConnectPtrToStructs(s.Connect)
return t
}
func NewServiceDefinitionFromStructs(t structs.ServiceDefinition) ServiceDefinition {
var s ServiceDefinition
s.Kind = t.Kind
s.ID = t.ID
s.Name = t.Name
s.Tags = t.Tags
s.Address = t.Address
s.TaggedAddresses = NewMapStringServiceAddressFromStructs(t.TaggedAddresses)
s.Meta = t.Meta
s.Port = int32(t.Port)
s.Check = NewCheckTypeFromStructs(t.Check)
s.Checks = NewCheckTypesFromStructs(t.Checks)
s.Weights = NewWeightsPtrFromStructs(t.Weights)
s.Token = t.Token
s.EnableTagOverride = t.EnableTagOverride
s.Proxy = NewConnectProxyConfigPtrFromStructs(t.Proxy)
s.EnterpriseMeta = NewEnterpriseMetaFromStructs(t.EnterpriseMeta)
s.Connect = NewServiceConnectPtrFromStructs(t.Connect)
return s
}
func UpstreamToStructs(s Upstream) structs.Upstream {
var t structs.Upstream
t.DestinationType = s.DestinationType
t.DestinationNamespace = s.DestinationNamespace
t.DestinationName = s.DestinationName
t.Datacenter = s.Datacenter
t.LocalBindAddress = s.LocalBindAddress
t.LocalBindPort = int(s.LocalBindPort)
t.Config = ProtobufTypesStructToMapStringInterface(s.Config)
t.MeshGateway = MeshGatewayConfigToStructs(s.MeshGateway)
return t
}
func NewUpstreamFromStructs(t structs.Upstream) Upstream {
var s Upstream
s.DestinationType = t.DestinationType
s.DestinationNamespace = t.DestinationNamespace
s.DestinationName = t.DestinationName
s.Datacenter = t.Datacenter
s.LocalBindAddress = t.LocalBindAddress
s.LocalBindPort = int32(t.LocalBindPort)
s.Config = MapStringInterfaceToProtobufTypesStruct(t.Config)
s.MeshGateway = NewMeshGatewayConfigFromStructs(t.MeshGateway)
return s
}

View File

@ -61,7 +61,7 @@ type ConnectProxyConfig struct {
LocalServicePort int32 `protobuf:"varint,4,opt,name=LocalServicePort,proto3" json:"LocalServicePort,omitempty"`
// Config is the arbitrary configuration data provided with the proxy
// registration.
// mog: func-to=MapStringInterfaceToStructs func-from=NewMapStringInterfaceFromStructs
// mog: func-to=ProtobufTypesStructToMapStringInterface func-from=MapStringInterfaceToProtobufTypesStruct
Config *types.Struct `protobuf:"bytes,5,opt,name=Config,proto3" json:"Config,omitempty"`
// Upstreams describes any upstream dependencies the proxy instance should
// setup.
@ -142,7 +142,7 @@ type Upstream struct {
// Config is an opaque config that is specific to the proxy process being run.
// It can be used to pass arbitrary configuration for this specific upstream
// to the proxy.
// mog: func-to=MapStringInterfaceToStructs func-from=NewMapStringInterfaceFromStructs
// mog: func-to=ProtobufTypesStructToMapStringInterface func-from=MapStringInterfaceToProtobufTypesStruct
Config *types.Struct `protobuf:"bytes,7,opt,name=Config,proto3" json:"Config,omitempty"`
// MeshGateway is the configuration for mesh gateway usage of this upstream
MeshGateway MeshGatewayConfig `protobuf:"bytes,8,opt,name=MeshGateway,proto3" json:"MeshGateway"`

View File

@ -56,7 +56,7 @@ message ConnectProxyConfig {
// Config is the arbitrary configuration data provided with the proxy
// registration.
// mog: func-to=MapStringInterfaceToStructs func-from=NewMapStringInterfaceFromStructs
// mog: func-to=ProtobufTypesStructToMapStringInterface func-from=MapStringInterfaceToProtobufTypesStruct
google.protobuf.Struct Config = 5 [(gogoproto.nullable) = true];
// Upstreams describes any upstream dependencies the proxy instance should
@ -111,7 +111,7 @@ message Upstream {
// Config is an opaque config that is specific to the proxy process being run.
// It can be used to pass arbitrary configuration for this specific upstream
// to the proxy.
// mog: func-to=MapStringInterfaceToStructs func-from=NewMapStringInterfaceFromStructs
// mog: func-to=ProtobufTypesStructToMapStringInterface func-from=MapStringInterfaceToProtobufTypesStruct
google.protobuf.Struct Config = 7 [(gogoproto.nullable) = true];
// MeshGateway is the configuration for mesh gateway usage of this upstream