good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
upper
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
open
upper
Commits
912a42d5
Commit
912a42d5
authored
12 years ago
by
José Carlos Nieto
Browse files
Options
Downloads
Patches
Plain Diff
Adding methods for getting specific YAML value types.
parent
e2f8df60
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
yaml/yaml.go
+31
-6
31 additions, 6 deletions
yaml/yaml.go
yaml/yaml_test.go
+28
-0
28 additions, 0 deletions
yaml/yaml_test.go
with
59 additions
and
6 deletions
yaml/yaml.go
+
31
−
6
View file @
912a42d5
...
...
@@ -51,12 +51,37 @@ func Open(file string) *Yaml {
return
yaml
}
// Returns a YAML setting (or nil if the referred name does not exists). Read nested values by using a dot (.) between labels.
// Returns the string value of the YAML path or an empty string, if the path cannot be found.
func
(
y
*
Yaml
)
GetString
(
path
string
)
string
{
return
y
.
Get
(
path
,
""
)
.
(
string
)
}
// Returns the integer value of the YAML path or 0, if the path cannot be found.
func
(
y
*
Yaml
)
GetInt
(
path
string
)
int
{
return
y
.
Get
(
path
,
0
)
.
(
int
)
}
// Returns the float value of the YAML path or 0.0, if the path cannot be found.
func
(
y
*
Yaml
)
GetFloat
(
path
string
)
float64
{
return
y
.
Get
(
path
,
0
)
.
(
float64
)
}
// Returns the boolean value of the YAML path or false, if the path cannot be found.
func
(
y
*
Yaml
)
GetBool
(
path
string
)
bool
{
return
y
.
Get
(
path
,
false
)
.
(
bool
)
}
// Returns the sequenced value of the YAML path or an empty sequence, if the path cannot be found.
func
(
y
*
Yaml
)
GetSequence
(
path
string
)
[]
interface
{}
{
return
y
.
Get
(
path
,
nil
)
.
([]
interface
{})
}
// Returns a YAML setting (or defaultValue if the referred name does not exists). Read nested values by using a dot (.) between labels.
//
// Example:
//
// yaml.Get("foo.bar")
func
(
y
*
Yaml
)
Get
(
path
string
,
def
interface
{})
interface
{}
{
// yaml.Get("foo.bar"
, "default"
)
func
(
y
*
Yaml
)
Get
(
path
string
,
def
aultValue
interface
{})
interface
{}
{
var
p
Tuple
path
=
strings
.
ToLower
(
path
)
...
...
@@ -85,17 +110,17 @@ func (y *Yaml) Get(path string, def interface{}) interface{} {
}
default
:
{
return
def
return
def
aultValue
}
}
}
else
{
return
def
return
def
aultValue
}
}
}
return
def
return
def
aultValue
}
// Sets a YAML setting, use dots (.) to nest values inside values.
...
...
This diff is collapsed.
Click to expand it.
yaml/yaml_test.go
+
28
−
0
View file @
912a42d5
...
...
@@ -37,6 +37,34 @@ func TestGet(t *testing.T) {
t
.
Errorf
(
"Got %t expecting %t."
,
val4
,
test4
)
}
test5
:=
"Hello World!"
val5
:=
settings
.
GetString
(
"test_string"
)
if
test5
!=
val5
{
t
.
Errorf
(
"Got %t expecting %t."
,
test5
,
val5
)
}
test6
:=
1234
val6
:=
settings
.
GetInt
(
"test_int"
)
if
test6
!=
val6
{
t
.
Errorf
(
"Got %t expecting %t."
,
test6
,
val6
)
}
test7
:=
1.2
val7
:=
settings
.
GetFloat
(
"test_float"
)
if
test7
!=
val7
{
t
.
Errorf
(
"Got %t expecting %t."
,
test7
,
val7
)
}
test8
:=
true
val8
:=
settings
.
GetBool
(
"test_bool"
)
if
test8
!=
val8
{
t
.
Errorf
(
"Got %t expecting %t."
,
test8
,
val8
)
}
}
func
TestSet
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment