Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sidecar
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
sidecar
Commits
f2ad2147
Commit
f2ad2147
authored
Sep 24, 2021
by
suyanlong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add defaults value function
parent
c06463db
Pipeline
#8057
failed with stages
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
72 additions
and
5 deletions
+72
-5
go.mod
go.mod
+1
-0
go.sum
go.sum
+2
-0
defaults.go
tool/defaults.go
+10
-0
defaults_test.go
tool/defaults_test.go
+52
-0
tool.go
tool/tool.go
+7
-5
No files found.
go.mod
View file @
f2ad2147
...
...
@@ -43,6 +43,7 @@ require (
github.com/bxcodec/faker/v3 v3.6.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/creasty/defaults v1.5.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20190912175916-7055855a373f // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
...
...
go.sum
View file @
f2ad2147
...
...
@@ -97,6 +97,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSY
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creasty/defaults v1.5.2 h1:/VfB6uxpyp6h0fr7SPp7n8WJBoV8jfxQXPCnkVSjyls=
github.com/creasty/defaults v1.5.2/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
...
...
tool/defaults.go
0 → 100644
View file @
f2ad2147
package
tool
import
(
"github.com/creasty/defaults"
)
var
(
Default
=
defaults
.
Set
MustDefault
=
defaults
.
MustSet
)
tool/defaults_test.go
0 → 100644
View file @
f2ad2147
package
tool
import
(
"math/rand"
"testing"
"github.com/creasty/defaults"
"github.com/stretchr/testify/assert"
)
type
Gender
string
type
Sample
struct
{
Name
string
`default:"John Smith"`
Age
int
`default:"27"`
Gender
Gender
`default:"m"`
Slice
[]
string
`default:"[]"`
SliceByJSON
[]
int
`default:"[1, 2, 3]"`
// Supports JSON
Map
map
[
string
]
int
`default:"{}"`
MapByJSON
map
[
string
]
int
`default:"{\"foo\": 123}"`
Struct
OtherStruct
`default:"{}"`
StructPtr
*
OtherStruct
`default:"{\"Foo\": 123}"`
NoTag
OtherStruct
// Recurses into a nested struct by default
OptOut
OtherStruct
`default:"-"`
// Opt-out
}
type
OtherStruct
struct
{
Hello
string
`default:"world"`
// Tags in a nested struct also work
Foo
int
`default:"-"`
Random
int
`default:"-"`
}
// SetDefaults implements defaults.Setter interface
func
(
s
*
OtherStruct
)
SetDefaults
()
{
if
defaults
.
CanUpdate
(
s
.
Random
)
{
// Check if it's a zero value (recommended)
s
.
Random
=
rand
.
Int
()
// Set a dynamic value
}
}
func
TestDefaults
(
t
*
testing
.
T
)
{
obj
:=
&
Sample
{}
err
:=
Default
(
obj
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
obj
.
Age
,
27
)
assert
.
Equal
(
t
,
obj
.
Gender
,
Gender
(
"m"
))
assert
.
Equal
(
t
,
obj
.
Slice
,
[]
string
{})
assert
.
Equal
(
t
,
obj
.
Map
,
map
[
string
]
int
{})
assert
.
Equal
(
t
,
obj
.
MapByJSON
,
map
[
string
]
int
{
"foo"
:
123
})
}
tool/tool.go
View file @
f2ad2147
...
...
@@ -36,8 +36,10 @@ func Exist(path string) bool {
return
false
}
//https://github.com/fatih/gomodifytags
//https://github.com/favadi/protoc-go-inject-tag
//https://github.com/0xAX/go-algorithms
//https://github.com/emirpasic/gods
//
// https://github.com/fatih/gomodifytags
// https://github.com/favadi/protoc-go-inject-tag
// https://github.com/0xAX/go-algorithms
// https://github.com/emirpasic/gods
// https://github.com/gookit/goutil
// https://github.com/gookit/color
// https://github.com/creasty/defaults
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