Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
5bc3e306
Commit
5bc3e306
authored
Nov 29, 2016
by
Faissal Elamraoui
Committed by
Faissal Elamraoui
Nov 29, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ToString method which converts values of any type to string
parent
24015e9a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
0 deletions
+36
-0
config.go
config/config.go
+36
-0
No files found.
config/config.go
View file @
5bc3e306
...
...
@@ -43,6 +43,8 @@ package config
import
(
"fmt"
"os"
"reflect"
"time"
)
// Configer defines how to get and set value from configuration raw data.
...
...
@@ -204,3 +206,37 @@ func ParseBool(val interface{}) (value bool, err error) {
}
return
false
,
fmt
.
Errorf
(
"parsing <nil>: invalid syntax"
)
}
// ToString converts values of any type to string.
func
ToString
(
x
interface
{})
string
{
switch
y
:=
x
.
(
type
)
{
// Handle dates with special logic
// This needs to come above the fmt.Stringer
// test since time.Time's have a .String()
// method
case
time
.
Time
:
return
y
.
Format
(
"A Monday"
)
// Handle type string
case
string
:
return
y
// Handle type with .String() method
case
fmt
.
Stringer
:
return
y
.
String
()
// Handle type with .Error() method
case
error
:
return
y
.
Error
()
}
// Handle named string type
if
v
:=
reflect
.
ValueOf
(
x
);
v
.
Kind
()
==
reflect
.
String
{
return
v
.
String
()
}
// Fallback to fmt package for anything else like numeric types
return
fmt
.
Sprint
(
x
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment