add some initialisers

This commit is contained in:
Asim Aslam 2019-07-09 16:37:59 +01:00
parent 6bdc23a3aa
commit d725980444
2 changed files with 29 additions and 2 deletions

View File

@ -18,6 +18,9 @@ type network struct {
// name of the network // name of the network
name string name string
// network address used where one is specified
address string
// transport // transport
transport transport.Transport transport transport.Transport
} }
@ -34,15 +37,16 @@ type listener struct {
} }
func (n *network) Create() (*Node, error) { func (n *network) Create() (*Node, error) {
ip, err := addr.Extract("") ip, err := addr.Extract(n.address)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Node{ return &Node{
Id: fmt.Sprintf("%s-%s", n.name, uuid.New().String()), Id: fmt.Sprintf("%s-%s", n.name, uuid.New().String()),
Address: ip, Address: ip,
Network: n.Name(),
Metadata: map[string]string{ Metadata: map[string]string{
"network": n.Name(), "network": n.String(),
"transport": n.transport.String(), "transport": n.transport.String(),
}, },
}, nil }, nil
@ -140,6 +144,12 @@ func newNetwork(opts ...options.Option) *network {
net.name = name.(string) net.name = name.(string)
} }
// get network name
address, ok := options.Values().Get("network.address")
if ok {
net.address = address.(string)
}
// get network transport // get network transport
t, ok := options.Values().Get("network.transport") t, ok := options.Values().Get("network.transport")
if ok { if ok {

View File

@ -3,6 +3,7 @@ package network
import ( import (
"github.com/micro/go-micro/config/options" "github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/network/transport"
) )
// Network defines a network interface. The network is a single // Network defines a network interface. The network is a single
@ -25,6 +26,7 @@ type Network interface {
type Node struct { type Node struct {
Id string Id string
Address string Address string
Network string
Metadata map[string]string Metadata map[string]string
} }
@ -69,3 +71,18 @@ var (
func NewNetwork(opts ...options.Option) Network { func NewNetwork(opts ...options.Option) Network {
return newNetwork(opts...) return newNetwork(opts...)
} }
// Name sets the network name
func Name(n string) options.Option {
return options.WithValue("network.name", n)
}
// Address sets the network address
func Address(a string) options.Option {
return options.WithValue("network.address", a)
}
// Transport sets the network transport
func Transport(t transport.Transport) options.Option {
return options.WithValue("network.transport", t)
}