Skip to content

Commit

Permalink
Merge branch 'master' into sasha/vars
Browse files Browse the repository at this point in the history
  • Loading branch information
klizhentas authored Jun 10, 2016
2 parents fba07c4 + a637a7d commit b3a105e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/utils/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ func (a *NetAddr) String() string {
return a.Addr
}

// Network returns the scheme for this network address (tcp or unix)
func (a *NetAddr) Network() string {
return a.AddrNetwork
}

// MarshalYAML defines how a network address should be marshalled to a string
func (a *NetAddr) MarshalYAML() (interface{}, error) {
url := url.URL{Scheme: a.AddrNetwork, Host: a.Addr, Path: a.Path}
return strings.TrimLeft(url.String(), "/"), nil
}

// UnmarshalYAML defines how a string can be unmarshalled into a network address
func (a *NetAddr) UnmarshalYAML(unmarshal func(interface{}) error) error {
var addr string
err := unmarshal(&addr)
Expand Down Expand Up @@ -121,6 +129,15 @@ func ParseAddr(a string) (*NetAddr, error) {
}
}

// MustParseAddr parses the provided string into NetAddr or panics on an error
func MustParseAddr(a string) *NetAddr {
addr, err := ParseAddr(a)
if err != nil {
panic(fmt.Sprintf("failed to parse %v: %v", a, err))
}
return addr
}

// ParseHostPortAddr takes strings like "host:port" and returns
// *NetAddr or an error
//
Expand Down
42 changes: 41 additions & 1 deletion lib/utils/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package utils

import (
"os"
"strings"
"testing"

. "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
)

func TestAddrSturct(t *testing.T) { TestingT(t) }
func TestAddrStruct(t *testing.T) { TestingT(t) }

type AddrTestSuite struct {
}
Expand Down Expand Up @@ -134,3 +136,41 @@ func (s *AddrTestSuite) TestGuess(c *C) {
c.Assert(ip[12] == 10 || ip[12] == 192 || ip[12] == 172, Equals, true)
}
}

func (s *AddrTestSuite) TestMarshal(c *C) {
testCases := []struct {
in *NetAddr
expected string
}{
{in: &NetAddr{Addr: "localhost:5000"}, expected: "localhost:5000"},
{in: &NetAddr{AddrNetwork: "tcp", Addr: "localhost:5000"}, expected: "tcp://localhost:5000"},
{in: &NetAddr{AddrNetwork: "tcp", Addr: "localhost:5000", Path: "/path"}, expected: "tcp://localhost:5000/path"},
{in: &NetAddr{AddrNetwork: "unix", Path: "/path"}, expected: "unix:///path"},
}

for i, testCase := range testCases {
bytes, err := yaml.Marshal(testCase.in)
c.Assert(err, IsNil)
c.Assert(strings.TrimSpace(string(bytes)), Equals, testCase.expected,
Commentf("test case %v, %v should be marshalled to: %v", i, testCase.in, testCase.expected))
}
}

func (s *AddrTestSuite) TestUnmarshal(c *C) {
testCases := []struct {
in string
expected *NetAddr
}{
{in: "localhost:5000", expected: &NetAddr{AddrNetwork: "tcp", Addr: "localhost:5000"}},
{in: "tcp://localhost:5000/path", expected: &NetAddr{AddrNetwork: "tcp", Addr: "localhost:5000", Path: "/path"}},
{in: "unix:///path", expected: &NetAddr{AddrNetwork: "unix", Addr: "/path"}},
}

for i, testCase := range testCases {
addr := &NetAddr{}
err := yaml.Unmarshal([]byte(testCase.in), addr)
c.Assert(err, IsNil)
c.Assert(addr, DeepEquals, testCase.expected,
Commentf("test case %v, %v should be unmarshalled to: %v", i, testCase.in, testCase.expected))
}
}

0 comments on commit b3a105e

Please sign in to comment.