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
04eecf81
Commit
04eecf81
authored
1 year ago
by
ko_oler
Browse files
Options
Downloads
Patches
Plain Diff
правки по ПР
parent
e20e0226
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/schema/field/timestamp.go
+37
-14
37 additions, 14 deletions
pkg/schema/field/timestamp.go
pkg/schema/field/timestamp_test.go
+12
-3
12 additions, 3 deletions
pkg/schema/field/timestamp_test.go
with
49 additions
and
17 deletions
pkg/schema/field/timestamp.go
+
37
−
14
View file @
04eecf81
...
@@ -2,11 +2,11 @@ package field
...
@@ -2,11 +2,11 @@ package field
import
(
import
(
"context"
"context"
"errors"
"fmt"
"fmt"
"reflect"
"reflect"
"strconv"
"time"
"strings"
"git.perx.ru/perxis/perxis-go/pkg/errors"
)
)
var
timestampType
=
&
TimestampType
{}
var
timestampType
=
&
TimestampType
{}
...
@@ -37,22 +37,45 @@ func (TimestampType) Decode(_ context.Context, _ *Field, v interface{}) (interfa
...
@@ -37,22 +37,45 @@ func (TimestampType) Decode(_ context.Context, _ *Field, v interface{}) (interfa
switch
i
:=
v
.
(
type
)
{
switch
i
:=
v
.
(
type
)
{
case
string
:
case
string
:
t
:=
strings
.
Split
(
i
,
":"
)
duration
,
err
:=
time
.
ParseDuration
(
i
)
if
len
(
t
)
==
3
{
if
err
!=
nil
{
hourToSeconds
,
_
:=
strconv
.
Atoi
(
t
[
0
])
t
,
err
:=
time
.
Parse
(
time
.
TimeOnly
,
i
)
minutesToSeconds
,
_
:=
strconv
.
Atoi
(
t
[
1
])
if
err
!=
nil
{
seconds
,
_
:=
strconv
.
Atoi
(
t
[
2
])
return
nil
,
errors
.
Wrap
(
err
,
"wrong format"
)
return
int64
(
hourToSeconds
*
3600
+
minutesToSeconds
*
60
+
seconds
),
nil
}
return
t
.
Sub
(
time
.
Date
(
t
.
Year
(),
t
.
Month
(),
t
.
Day
(),
0
,
0
,
0
,
0
,
t
.
Location
()))
.
Nanoseconds
(),
nil
}
return
duration
.
Nanoseconds
(),
nil
default
:
v
,
err
:=
ToInt
(
i
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"TimestampField decode error: unsupported value type :
\"
%s
\"
"
,
reflect
.
ValueOf
(
v
)
.
Kind
())
}
}
return
nil
,
errors
.
New
(
"wrong time format: input in 'HH:MM:SS'"
)
return
v
,
nil
}
}
func
ToInt
(
i
interface
{})
(
interface
{},
error
)
{
switch
v
:=
i
.
(
type
)
{
case
int64
:
case
int64
:
return
i
,
nil
return
v
,
nil
case
int
:
return
int64
(
v
),
nil
case
int8
:
return
int64
(
v
),
nil
case
int32
:
return
int64
(
v
),
nil
case
uint64
:
case
uint64
:
return
int64
(
i
),
nil
return
v
,
nil
case
uint
:
return
uint64
(
v
),
nil
case
uint8
:
return
uint64
(
v
),
nil
case
uint32
:
return
uint64
(
v
),
nil
default
:
return
0
,
errors
.
Errorf
(
"error convert
\"
%s
\"
to timestamp"
,
i
)
}
}
return
nil
,
fmt
.
Errorf
(
"TimestampField decode error: unsupported value type :
\"
%s
\"
"
,
reflect
.
ValueOf
(
v
)
.
Kind
())
}
}
func
(
TimestampType
)
Encode
(
_
context
.
Context
,
_
*
Field
,
v
interface
{})
(
interface
{},
error
)
{
func
(
TimestampType
)
Encode
(
_
context
.
Context
,
_
*
Field
,
v
interface
{})
(
interface
{},
error
)
{
...
...
This diff is collapsed.
Click to expand it.
pkg/schema/field/timestamp_test.go
+
12
−
3
View file @
04eecf81
...
@@ -14,14 +14,23 @@ func TestTimestamp_Decode(t *testing.T) {
...
@@ -14,14 +14,23 @@ func TestTimestamp_Decode(t *testing.T) {
want
interface
{}
want
interface
{}
wantErr
bool
wantErr
bool
}{
}{
{
"Correct"
,
Timestamp
(),
int64
(
2
),
int64
(
2
),
false
},
// #0
{
"Correct"
,
Timestamp
(),
int64
(
2
),
int64
(
2
),
false
},
// #0
{
"Correct"
,
Timestamp
(),
uint64
(
2
),
int64
(
2
),
false
},
// #1
{
"Correct"
,
Timestamp
(),
uint64
(
2
),
uint64
(
2
),
false
},
// #1
{
"Correct"
,
Timestamp
(),
"13:10:44"
,
int64
(
47444
),
false
},
// #2
{
"Correct"
,
Timestamp
(),
2
,
int64
(
2
),
false
},
// #2
{
"Correct"
,
Timestamp
(),
"13h10m44s"
,
int64
(
47444000000000
),
false
},
// #3
{
"Correct"
,
Timestamp
(),
"24h"
,
int64
(
86400000000000
),
false
},
// #4
{
"Correct"
,
Timestamp
(),
"2.5h"
,
int64
(
9000000000000
),
false
},
// #5
{
"Correct"
,
Timestamp
(),
"-5h"
,
int64
(
-
18000000000000
),
false
},
// #5
{
"Correct"
,
Timestamp
(),
"13:10:44"
,
int64
(
47444000000000
),
false
},
// #6
{
"Correct"
,
Timestamp
(),
"23:59:59"
,
int64
(
86399000000000
),
false
},
// #7
{
"Correct"
,
Timestamp
(),
"00:00:00"
,
int64
(
0
),
false
},
// #8
{
"Correct"
,
Timestamp
(),
"00:00:01"
,
int64
(
1000000000
),
false
},
// #8
{
"Wrong data"
,
Timestamp
(),
""
,
nil
,
true
},
// #0
{
"Wrong data"
,
Timestamp
(),
""
,
nil
,
true
},
// #0
{
"Wrong data"
,
Timestamp
(),
[]
byte
(
""
),
nil
,
true
},
// #1
{
"Wrong data"
,
Timestamp
(),
[]
byte
(
""
),
nil
,
true
},
// #1
{
"Wrong data"
,
Timestamp
(),
2.2
,
nil
,
true
},
// #2
{
"Wrong data"
,
Timestamp
(),
2.2
,
nil
,
true
},
// #2
{
"Wrong data"
,
Timestamp
(),
"13:10"
,
nil
,
true
},
// #3
{
"Wrong data"
,
Timestamp
(),
"13:10"
,
nil
,
true
},
// #3
{
"Wrong data"
,
Timestamp
(),
"24:00:00"
,
nil
,
true
},
// #4
}
}
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
...
...
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