Skip to content

Commit 08a3dda

Browse files
committed
feat(profile): add String()
Add String() to Profile. Profile already has Get(), but that can be awkward to use since you need to check the error. A lot of the time that is unnecessary since you're comparing the string to a specific value; besides the error can only occur when the profile is uninitialized. Signed-off-by: setrofim <setrofim@gmail.com>
1 parent 9ac86e7 commit 08a3dda

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎profile.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ func (s *Profile) Set(urlOrOID string) error {
6161
return s.decodeProfileFromString(urlOrOID)
6262
}
6363

64-
// Get returns the profile as string (URI or dotted-decimal OID)
64+
// Get returns the profile as string (URI or dotted-decimal OID). If the
65+
// profile is invalid, an error is returned.
6566
func (s Profile) Get() (string, error) {
6667
switch t := s.val.(type) {
6768
case *url.URL:
@@ -73,6 +74,13 @@ func (s Profile) Get() (string, error) {
7374
}
7475
}
7576

77+
// Get returns the profile as string (URI or dotted-decimal OID). If the
78+
// profile is invalid, an empty string is returned.
79+
func (s Profile) String() string {
80+
ret, _ := s.Get()
81+
return ret
82+
}
83+
7684
// IsURI checks whether a stored profile is a URI
7785
func (s Profile) IsURI() bool {
7886
_, ok := s.val.(*url.URL)

‎profile_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,14 @@ func TestMustNewProfile(t *testing.T) {
364364
_ = MustNewProfile("@@@")
365365
})
366366
}
367+
368+
func TestProfile_String(t *testing.T) {
369+
profile := MustNewProfile("1.2.3.4")
370+
assert.Equal(t, "1.2.3.4", profile.String())
371+
372+
profile = MustNewProfile("http://example.com")
373+
assert.Equal(t, "http://example.com", profile.String())
374+
375+
profile = &Profile{}
376+
assert.Equal(t, "", profile.String())
377+
}

0 commit comments

Comments
 (0)