Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package Registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
c7a05319
Commit
c7a05319
authored
1 year ago
by
ko_oler
Browse files
Options
Downloads
Patches
Plain Diff
правки по ПР
parent
d7fbac07
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pkg/id/id.go
+14
-18
14 additions, 18 deletions
pkg/id/id.go
pkg/id/id_test.go
+53
-3
53 additions, 3 deletions
pkg/id/id_test.go
with
67 additions
and
21 deletions
pkg/id/id.go
+
14
−
18
View file @
c7a05319
...
@@ -14,11 +14,11 @@ const (
...
@@ -14,11 +14,11 @@ const (
)
)
var
(
var
(
ErrEmptyID
=
errors
.
New
(
"empty
ID
"
)
ErrEmptyID
=
errors
.
New
(
"empty
id
"
)
ErrNotValidID
=
errors
.
New
(
"id contains invalid characters"
)
ErrNotValidID
=
errors
.
New
(
"id contains invalid characters"
)
ErrLongID
=
errors
.
New
(
"id too long (max
64
symbols)"
)
ErrLongID
=
errors
.
Errorf
(
"id too long (max
%d
symbols)"
,
MaxLengthID
)
ErrEmptyName
=
errors
.
New
(
"empty name"
)
ErrEmptyName
=
errors
.
New
(
"empty name"
)
ErrLongName
=
errors
.
New
(
"name too long (max
256
symbols)"
)
ErrLongName
=
errors
.
Errorf
(
"name too long (max
%d
symbols)"
,
MaxLengthName
)
isValidID
=
regexp
.
MustCompile
(
`^[A-Za-z][A-Za-z0-9-_]*$`
)
.
MatchString
isValidID
=
regexp
.
MustCompile
(
`^[A-Za-z][A-Za-z0-9-_]*$`
)
.
MatchString
)
)
...
@@ -26,31 +26,27 @@ func GenerateNewID() string {
...
@@ -26,31 +26,27 @@ func GenerateNewID() string {
return
xid
.
New
()
.
String
()
return
xid
.
New
()
.
String
()
}
}
func
ValidateID
(
id
*
string
)
error
{
func
ValidateID
(
id
string
)
(
string
,
error
)
{
trimmedID
:=
*
id
trimmedID
:=
strings
.
TrimSpace
(
id
)
trimmedID
=
strings
.
TrimSpace
(
trimmedID
)
if
trimmedID
==
""
{
if
trimmedID
==
""
{
return
ErrEmptyID
return
trimmedID
,
ErrEmptyID
}
}
if
len
(
trimmedID
)
>
MaxLengthID
{
if
len
(
trimmedID
)
>
MaxLengthID
{
return
ErrLongID
return
trimmedID
,
ErrLongID
}
}
if
!
isValidID
(
trimmedID
)
{
if
!
isValidID
(
trimmedID
)
{
return
errors
.
WithDetail
(
ErrNotValidID
,
"must begin with latin letters and contain latin letters, numbers or '-', '_'"
)
return
trimmedID
,
errors
.
WithDetail
(
ErrNotValidID
,
"must begin with latin letters and contain latin letters, numbers or '-', '_'"
)
}
}
*
id
=
trimmedID
return
trimmedID
,
nil
return
nil
}
}
func
ValidateName
(
name
*
string
)
error
{
func
ValidateName
(
name
string
)
(
string
,
error
)
{
trimmedName
:=
*
name
trimmedName
:=
strings
.
TrimSpace
(
name
)
trimmedName
=
strings
.
TrimSpace
(
trimmedName
)
if
trimmedName
==
""
{
if
trimmedName
==
""
{
return
ErrEmptyName
return
trimmedName
,
ErrEmptyName
}
}
if
len
(
trimmedName
)
>
MaxLengthName
{
if
len
(
trimmedName
)
>
MaxLengthName
{
return
ErrLongName
return
trimmedName
,
ErrLongName
}
}
*
name
=
trimmedName
return
trimmedName
,
nil
return
nil
}
}
This diff is collapsed.
Click to expand it.
pkg/id/id_test.go
+
53
−
3
View file @
c7a05319
package
id
package
id
import
"testing"
import
(
"testing"
"github.com/stretchr/testify/require"
)
func
TestValidateID
(
t
*
testing
.
T
)
{
func
TestValidateID
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
tests
:=
[]
struct
{
name
string
name
string
id
string
id
string
result
string
wantErr
bool
wantErr
bool
}{
}{
{
{
"Correct ID #1"
,
"Correct ID #1"
,
"test"
,
"test"
,
"test"
,
false
,
false
,
},
},
{
{
"Correct ID #2"
,
"Correct ID #2"
,
"test_1"
,
"test_1"
,
"test_1"
,
false
,
false
,
},
},
{
{
"Correct ID #3"
,
"Correct ID #3"
,
"test_1-2"
,
"test_1-2"
,
"test_1-2"
,
false
,
},
{
"Trimmed ID #1"
,
" test_1-2 "
,
"test_1-2"
,
false
,
},
{
"Trimmed ID #2"
,
"test_1-2 "
,
"test_1-2"
,
false
,
false
,
},
},
{
{
"Fail, ID starts with number"
,
"Fail, ID starts with number"
,
"1test"
,
"1test"
,
"1test"
,
true
,
true
,
},
},
{
{
"Fail, ID to long"
,
"Fail, ID to long"
,
"9QoulAlFbIcnQYLSudMistN1IczCWrXUTtN5EgNQJd516DN9UjXYJxieJ1RcsNcs1"
,
"9QoulAlFbIcnQYLSudMistN1IczCWrXUTtN5EgNQJd516DN9UjXYJxieJ1RcsNcs1"
,
"9QoulAlFbIcnQYLSudMistN1IczCWrXUTtN5EgNQJd516DN9UjXYJxieJ1RcsNcs1"
,
true
,
true
,
},
},
{
{
"Fail, ID with symbols"
,
"Fail, ID with symbols"
,
"test!_)(&&"
,
"test!_)(&&"
,
"test!_)(&&"
,
true
,
},
{
"Fail, ID is empty"
,
" "
,
""
,
true
,
true
,
},
},
}
}
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
if
err
:=
ValidateID
(
&
tt
.
id
);
(
err
!=
nil
)
!=
tt
.
wantErr
{
res
,
err
:=
ValidateID
(
tt
.
id
)
if
(
err
!=
nil
)
!=
tt
.
wantErr
{
t
.
Errorf
(
"ValidateID() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
t
.
Errorf
(
"ValidateID() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
}
}
require
.
Equal
(
t
,
tt
.
result
,
res
)
})
})
}
}
}
}
...
@@ -52,34 +83,53 @@ func TestValidateName(t *testing.T) {
...
@@ -52,34 +83,53 @@ func TestValidateName(t *testing.T) {
tests
:=
[]
struct
{
tests
:=
[]
struct
{
nameTest
string
nameTest
string
name
string
name
string
result
string
wantErr
bool
wantErr
bool
}{
}{
{
{
"Correct name #1"
,
"Correct name #1"
,
"test"
,
"test"
,
"test"
,
false
,
false
,
},
},
{
{
"Correct name #2"
,
"Correct name #2"
,
"test_1"
,
"test_1"
,
"test_1"
,
false
,
false
,
},
},
{
{
"Correct name #3"
,
"Correct name #3"
,
"test_1-2"
,
"test_1-2"
,
"test_1-2"
,
false
,
},
{
"Trimmed name #1"
,
" test_1-3 "
,
"test_1-3"
,
false
,
},
{
"Trimmed name #2"
,
"test_1234 "
,
"test_1234"
,
false
,
false
,
},
},
{
{
"Fail, name to long"
,
"Fail, name to long"
,
"ChKRLdvWi0wYYPazuBXrIRtNFy96qGrhBDkuKQNd6N2DPV86IGdXVkeTjWj7qezKreIFUp9IUn03A8WJTTORHkgXAvkPuDVM8tVMcnHbR2hznooJ3gGUsXpn4uXo2QhsviHPyUKmE10GnkCOv9FgAMILNoFVHnIiSHI3cjWlGJglpS9YAMXFB1phOIRF5yol3jmPE7EeU1uZPUw9C2PChuksGsOuJQov07Zom0b13r6wOJv8PZVa4IKmjDDLGKlq1"
,
"ChKRLdvWi0wYYPazuBXrIRtNFy96qGrhBDkuKQNd6N2DPV86IGdXVkeTjWj7qezKreIFUp9IUn03A8WJTTORHkgXAvkPuDVM8tVMcnHbR2hznooJ3gGUsXpn4uXo2QhsviHPyUKmE10GnkCOv9FgAMILNoFVHnIiSHI3cjWlGJglpS9YAMXFB1phOIRF5yol3jmPE7EeU1uZPUw9C2PChuksGsOuJQov07Zom0b13r6wOJv8PZVa4IKmjDDLGKlq1"
,
"ChKRLdvWi0wYYPazuBXrIRtNFy96qGrhBDkuKQNd6N2DPV86IGdXVkeTjWj7qezKreIFUp9IUn03A8WJTTORHkgXAvkPuDVM8tVMcnHbR2hznooJ3gGUsXpn4uXo2QhsviHPyUKmE10GnkCOv9FgAMILNoFVHnIiSHI3cjWlGJglpS9YAMXFB1phOIRF5yol3jmPE7EeU1uZPUw9C2PChuksGsOuJQov07Zom0b13r6wOJv8PZVa4IKmjDDLGKlq1"
,
true
,
true
,
},
},
}
}
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
if
err
:=
ValidateName
(
&
tt
.
name
);
(
err
!=
nil
)
!=
tt
.
wantErr
{
res
,
err
:=
ValidateName
(
tt
.
name
)
if
(
err
!=
nil
)
!=
tt
.
wantErr
{
t
.
Errorf
(
"ValidateName() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
t
.
Errorf
(
"ValidateName() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
}
}
require
.
Equal
(
t
,
tt
.
result
,
res
)
})
})
}
}
}
}
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