Susumu Yata
null+****@clear*****
Wed Jul 5 11:22:08 JST 2017
Susumu Yata 2017-07-05 11:22:08 +0900 (Wed, 05 Jul 2017) New Revision: 1bfe225544e16caf9260e89924f0ad21ca6829f6 https://github.com/groonga/grnci/commit/1bfe225544e16caf9260e89924f0ad21ca6829f6 Message: Add comments and rename constants. Modified files: v2/address.go v2/address_test.go Modified: v2/address.go (+36 -15) =================================================================== --- v2/address.go 2017-06-28 16:46:30 +0900 (21648ac) +++ v2/address.go 2017-07-05 11:22:08 +0900 (1240bad) @@ -22,14 +22,19 @@ type Address struct { // Address default settings. const ( - DefaultScheme = "gqtp" - DefaultHost = "localhost" - GQTPDefaultPort = 10043 - HTTPDefaultPort = 10041 - HTTPDefaultPath = "/d/" + // DefaultScheme is used to fill Address.Scheme if the scheme part is empty. + DefaultScheme = "gqtp" + // DefaultHost is used to fill Address.Host if the host part is empty. + DefaultHost = "localhost" + // DefaultGQTPPort is used to fill Address.Port if the scheme is GQTP and the port part is empty. + DefaultGQTPPort = 10043 + // HTTPDefaultPort is used to fill Address.Port if the scheme is HTTP and the port part is empty. + DefaultHTTPPort = 10041 + // HTTPDefaultPath is used to fill Address.Path if the scheme is HTTP and the path part is empty. + DefaultHTTPPath = "/d/" ) -// fillGQTP checks fields and fills missing fields in a GQTP address. +// fillGQTP fills missing fields in a GQTP address. func (a *Address) fillGQTP() error { if a.Scheme == "" { a.Scheme = "gqtp" @@ -50,7 +55,7 @@ func (a *Address) fillGQTP() error { a.Host = DefaultHost } if a.Port == 0 { - a.Port = GQTPDefaultPort + a.Port = DefaultGQTPPort } if a.Path != "" { return NewError(InvalidAddress, map[string]interface{}{ @@ -73,7 +78,7 @@ func (a *Address) fillGQTP() error { return nil } -// fillHTTP checks fields and fills missing fields in an HTTP address. +// fillHTTP fills missing fields in an HTTP address. func (a *Address) fillHTTP() error { if a.Scheme == "" { a.Scheme = "http" @@ -82,15 +87,15 @@ func (a *Address) fillHTTP() error { a.Host = DefaultHost } if a.Port == 0 { - a.Port = HTTPDefaultPort + a.Port = DefaultHTTPPort } if a.Path == "" { - a.Path = HTTPDefaultPath + a.Path = DefaultHTTPPath } return nil } -// fill checks fields and fills missing fields. +// fill fills missing fields. func (a *Address) fill() error { if a.Scheme == "" { a.Scheme = DefaultScheme @@ -113,7 +118,7 @@ func (a *Address) fill() error { return nil } -// parseHostPort parses a host and a port in an address. +// parseHostPort parses the host and port. func (a *Address) parseHostPort(s string) error { if s == "" { return nil @@ -163,7 +168,7 @@ func (a *Address) parseHostPort(s string) error { // parseAddress parses an address. // The expected address format is -// [scheme://][username[:password]@]host[:port][path]. +// [scheme://][username[:password]@][host][:port][path][?query][#fragment]. func parseAddress(s string) (*Address, error) { a := new(Address) if i := strings.IndexByte(s, '#'); i != -1 { @@ -202,6 +207,13 @@ func parseAddress(s string) (*Address, error) { // ParseAddress parses an address. // The expected address format is // [scheme://][username[:password]@][host][:port][path][?query][#fragment]. +// +// If the scheme part is empty, it is filled with DefaultScheme. +// If the host part is empty, it is filled with DefaultHost. +// If the port part is empty, it is filled with DefaultGQTPPort or +// DefaultHTTPPort according to the scheme. +// If the scheme is HTTP or HTTPS and the path part is empty, +// it is filled with DefaultHTTPPath. func ParseAddress(s string) (*Address, error) { a, err := parseAddress(s) if err != nil { @@ -215,6 +227,10 @@ func ParseAddress(s string) (*Address, error) { // ParseGQTPAddress parses a GQTP address. // The expected address format is [scheme://][host][:port]. +// +// If the scheme part is empty, it is filled with "gqtp". +// If the host part is empty, it is filled with DefaultHost. +// If the port part is empty, it is filled with DefaultGQTPPort. func ParseGQTPAddress(s string) (*Address, error) { a, err := parseAddress(s) if err != nil { @@ -234,9 +250,14 @@ func ParseGQTPAddress(s string) (*Address, error) { return a, nil } -// ParseHTTPAddress parses an HTTP address. +// ParseHTTPAddress parses an HTTP or HTTPS address. // The expected address format is // [scheme://][username[:password]@][host][:port][path][?query][#fragment]. +// +// If the scheme part is empty, it is filled with "http". +// If the host part is empty, it is filled with DefaultHost. +// If the port part is empty, it is filled with DefaultHTTPPort. +// If the path part is empty, it is filled with DefaultHTTPPath. func ParseHTTPAddress(s string) (*Address, error) { a, err := parseAddress(s) if err != nil { @@ -256,7 +277,7 @@ func ParseHTTPAddress(s string) (*Address, error) { return a, nil } -// String assembles the fields into an address. +// String assembles the fields into an address string. func (a *Address) String() string { var url string if a.Scheme != "" { Modified: v2/address_test.go (+15 -15) =================================================================== --- v2/address_test.go 2017-06-28 16:46:30 +0900 (e1d371a) +++ v2/address_test.go 2017-07-05 11:22:08 +0900 (7bc8764) @@ -9,15 +9,15 @@ import ( func TestParseAddress(t *testing.T) { data := map[string]string{ "": fmt.Sprintf("%s://%s:%d%s", - DefaultScheme, DefaultHost, GQTPDefaultPort, ""), + DefaultScheme, DefaultHost, DefaultGQTPPort, ""), "gqtp://": fmt.Sprintf("%s://%s:%d%s", - DefaultScheme, DefaultHost, GQTPDefaultPort, ""), + DefaultScheme, DefaultHost, DefaultGQTPPort, ""), "http://": fmt.Sprintf("%s://%s:%d%s", - "http", DefaultHost, HTTPDefaultPort, HTTPDefaultPath), + "http", DefaultHost, DefaultHTTPPort, DefaultHTTPPath), "https://": fmt.Sprintf("%s://%s:%d%s", - "https", DefaultHost, HTTPDefaultPort, HTTPDefaultPath), + "https", DefaultHost, DefaultHTTPPort, DefaultHTTPPath), "example.com": fmt.Sprintf("%s://%s:%d%s", - DefaultScheme, "example.com", GQTPDefaultPort, ""), + DefaultScheme, "example.com", DefaultGQTPPort, ""), ":8080": fmt.Sprintf("%s://%s:%d%s", DefaultScheme, DefaultHost, 8080, ""), } @@ -44,11 +44,11 @@ func TestParseAddress(t *testing.T) { func TestParseGQTPAddress(t *testing.T) { data := map[string]string{ "": fmt.Sprintf("%s://%s:%d%s", - "gqtp", DefaultHost, GQTPDefaultPort, ""), + "gqtp", DefaultHost, DefaultGQTPPort, ""), "gqtp://": fmt.Sprintf("%s://%s:%d%s", - "gqtp", DefaultHost, GQTPDefaultPort, ""), + "gqtp", DefaultHost, DefaultGQTPPort, ""), "example.com": fmt.Sprintf("%s://%s:%d%s", - "gqtp", "example.com", GQTPDefaultPort, ""), + "gqtp", "example.com", DefaultGQTPPort, ""), ":8080": fmt.Sprintf("%s://%s:%d%s", "gqtp", DefaultHost, 8080, ""), "example.com:8080": fmt.Sprintf("%s://%s:%d%s", @@ -77,21 +77,21 @@ func TestParseGQTPAddress(t *testing.T) { func TestParseHTTPAddress(t *testing.T) { data := map[string]string{ "": fmt.Sprintf("%s://%s:%d%s", - "http", DefaultHost, HTTPDefaultPort, HTTPDefaultPath), + "http", DefaultHost, DefaultHTTPPort, DefaultHTTPPath), "https://": fmt.Sprintf("%s://%s:%d%s", - "https", DefaultHost, HTTPDefaultPort, HTTPDefaultPath), + "https", DefaultHost, DefaultHTTPPort, DefaultHTTPPath), "example.com": fmt.Sprintf("%s://%s:%d%s", - "http", "example.com", HTTPDefaultPort, HTTPDefaultPath), + "http", "example.com", DefaultHTTPPort, DefaultHTTPPath), ":8080": fmt.Sprintf("%s://%s:%d%s", - "http", DefaultHost, 8080, HTTPDefaultPath), + "http", DefaultHost, 8080, DefaultHTTPPath), "http://example.com": fmt.Sprintf("%s://%s:%d%s", - "http", "example.com", HTTPDefaultPort, HTTPDefaultPath), + "http", "example.com", DefaultHTTPPort, DefaultHTTPPath), "http://example.com:8080": fmt.Sprintf("%s://%s:%d%s", - "http", "example.com", 8080, HTTPDefaultPath), + "http", "example.com", 8080, DefaultHTTPPath), "http://example.com:8080/": fmt.Sprintf("%s://%s:%d%s", "http", "example.com", 8080, "/"), "http://:8080": fmt.Sprintf("%s://%s:%d%s", - "http", DefaultHost, 8080, HTTPDefaultPath), + "http", DefaultHost, 8080, DefaultHTTPPath), "http://:8080/": fmt.Sprintf("%s://%s:%d%s", "http", DefaultHost, 8080, "/"), } -------------- next part -------------- HTML����������������������������... 下載