forked from kubernetes-retired/heapster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes-retired#413 from mvdan/uris-type
Uris type
- Loading branch information
Showing
16 changed files
with
349 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2015 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package manager | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type Uri struct { | ||
Key string | ||
Val url.URL | ||
} | ||
|
||
func (u *Uri) String() string { | ||
val := u.Val.String() | ||
if val == "" { | ||
return fmt.Sprintf("%s", u.Key) | ||
} | ||
return fmt.Sprintf("%s:%s", u.Key, val) | ||
} | ||
|
||
func (u *Uri) Set(value string) error { | ||
s := strings.SplitN(value, ":", 2) | ||
if s[0] == "" { | ||
return fmt.Errorf("missing uri key in '%s'", value) | ||
} | ||
u.Key = s[0] | ||
if len(s) > 1 && s[1] != "" { | ||
e := os.ExpandEnv(s[1]) | ||
uri, err := url.Parse(e) | ||
if err != nil { | ||
return err | ||
} | ||
u.Val = *uri | ||
} | ||
return nil | ||
} | ||
|
||
type Uris []Uri | ||
|
||
func (us *Uris) String() string { | ||
var b bytes.Buffer | ||
b.WriteString("[") | ||
for i, u := range *us { | ||
if i > 0 { | ||
b.WriteString(" ") | ||
} | ||
b.WriteString(u.String()) | ||
} | ||
b.WriteString("]") | ||
return b.String() | ||
} | ||
|
||
func (us *Uris) Set(value string) error { | ||
var u Uri | ||
if err := u.Set(value); err != nil { | ||
return err | ||
} | ||
*us = append(*us, u) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Copyright 2015 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package manager | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUriString(t *testing.T) { | ||
tests := [...]struct { | ||
in Uri | ||
want string | ||
}{ | ||
{ | ||
Uri{ | ||
Key: "gcm", | ||
}, | ||
"gcm", | ||
}, | ||
{ | ||
Uri{ | ||
Key: "influxdb", | ||
Val: url.URL{ | ||
Scheme: "http", | ||
Host: "monitoring-influxdb:8086", | ||
RawQuery: "key=val&key2=val2", | ||
}, | ||
}, | ||
"influxdb:http://monitoring-influxdb:8086?key=val&key2=val2", | ||
}, | ||
} | ||
for _, c := range tests { | ||
assert.Equal(t, c.want, c.in.String()) | ||
} | ||
} | ||
|
||
func TestUriSet(t *testing.T) { | ||
tests := [...]struct { | ||
in string | ||
want Uri | ||
wantErr bool | ||
}{ | ||
{"", Uri{}, true}, | ||
{":", Uri{}, true}, | ||
{":foo", Uri{}, true}, | ||
{"key:incorrecturl/%gh&%ij", Uri{}, true}, | ||
{ | ||
"gcm", | ||
Uri{Key: "gcm"}, | ||
false, | ||
}, | ||
{ | ||
"gcm:", | ||
Uri{Key: "gcm"}, | ||
false, | ||
}, | ||
{ | ||
"influxdb:http://monitoring-influxdb:8086?key=val&key2=val2", | ||
Uri{ | ||
Key: "influxdb", | ||
Val: url.URL{ | ||
Scheme: "http", | ||
Host: "monitoring-influxdb:8086", | ||
RawQuery: "key=val&key2=val2", | ||
}, | ||
}, | ||
false, | ||
}, | ||
} | ||
for _, c := range tests { | ||
var uri Uri | ||
err := uri.Set(c.in) | ||
if c.wantErr { | ||
assert.NotNil(t, err) | ||
} else { | ||
assert.Nil(t, err) | ||
assert.Equal(t, c.want, uri) | ||
} | ||
} | ||
} | ||
|
||
func TestUrisString(t *testing.T) { | ||
tests := [...]struct { | ||
in Uris | ||
want string | ||
}{ | ||
{ | ||
Uris{ | ||
Uri{Key: "gcm"}, | ||
}, | ||
"[gcm]", | ||
}, | ||
{ | ||
Uris{ | ||
Uri{Key: "gcm"}, | ||
Uri{ | ||
Key: "influxdb", | ||
Val: url.URL{Path: "foo"}, | ||
}, | ||
}, | ||
"[gcm influxdb:foo]", | ||
}, | ||
} | ||
for _, c := range tests { | ||
assert.Equal(t, c.want, c.in.String()) | ||
} | ||
} | ||
|
||
func TestUrisSet(t *testing.T) { | ||
tests := [...]struct { | ||
in []string | ||
want Uris | ||
wantErr bool | ||
}{ | ||
{[]string{""}, Uris{}, true}, | ||
{[]string{":foo"}, Uris{}, true}, | ||
{ | ||
[]string{"gcm"}, | ||
Uris{ | ||
Uri{Key: "gcm"}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
[]string{"gcm", "influxdb:foo"}, | ||
Uris{ | ||
Uri{Key: "gcm"}, | ||
Uri{ | ||
Key: "influxdb", | ||
Val: url.URL{Path: "foo"}, | ||
}, | ||
}, | ||
false, | ||
}, | ||
} | ||
for _, c := range tests { | ||
var uris Uris | ||
var err error | ||
for _, s := range c.in { | ||
if err = uris.Set(s); err != nil { | ||
break | ||
} | ||
} | ||
if c.wantErr { | ||
assert.NotNil(t, err) | ||
} else { | ||
assert.Nil(t, err) | ||
assert.Equal(t, c.want, uris) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.