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
e20e0226
Commit
e20e0226
authored
1 year ago
by
ko_oler
Browse files
Options
Downloads
Patches
Plain Diff
Реализован тип поля Timestamp в perxis schema
parent
4b1fdca3
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
+70
-0
70 additions, 0 deletions
pkg/schema/field/timestamp.go
pkg/schema/field/timestamp_test.go
+69
-0
69 additions, 0 deletions
pkg/schema/field/timestamp_test.go
with
139 additions
and
0 deletions
pkg/schema/field/timestamp.go
0 → 100644
+
70
−
0
View file @
e20e0226
package
field
import
(
"context"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
var
timestampType
=
&
TimestampType
{}
type
TimestampParameters
struct
{}
func
(
t
TimestampParameters
)
Type
()
Type
{
return
timestampType
}
func
(
t
*
TimestampParameters
)
Clone
(
reset
bool
)
Parameters
{
return
t
}
type
TimestampType
struct
{}
func
(
t
TimestampType
)
Name
()
string
{
return
"timestamp"
}
func
(
TimestampType
)
NewParameters
()
Parameters
{
return
&
TimestampParameters
{}
}
func
(
TimestampType
)
IsEmpty
(
v
interface
{})
bool
{
return
v
==
0
||
v
==
nil
}
func
(
TimestampType
)
Decode
(
_
context
.
Context
,
_
*
Field
,
v
interface
{})
(
interface
{},
error
)
{
if
v
==
nil
{
return
nil
,
nil
}
switch
i
:=
v
.
(
type
)
{
case
string
:
t
:=
strings
.
Split
(
i
,
":"
)
if
len
(
t
)
==
3
{
hourToSeconds
,
_
:=
strconv
.
Atoi
(
t
[
0
])
minutesToSeconds
,
_
:=
strconv
.
Atoi
(
t
[
1
])
seconds
,
_
:=
strconv
.
Atoi
(
t
[
2
])
return
int64
(
hourToSeconds
*
3600
+
minutesToSeconds
*
60
+
seconds
),
nil
}
return
nil
,
errors
.
New
(
"wrong time format: input in 'HH:MM:SS'"
)
case
int64
:
return
i
,
nil
case
uint64
:
return
int64
(
i
),
nil
}
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
)
{
if
v
==
nil
{
return
nil
,
nil
}
if
_
,
ok
:=
v
.
(
int64
);
ok
{
return
v
,
nil
}
return
nil
,
fmt
.
Errorf
(
"TimestampField encode error: unsupported value type :
\"
%s
\"
"
,
reflect
.
ValueOf
(
v
)
.
Kind
())
}
func
Timestamp
(
o
...
interface
{})
*
Field
{
return
NewField
(
&
TimestampParameters
{},
o
...
)
}
This diff is collapsed.
Click to expand it.
pkg/schema/field/timestamp_test.go
0 → 100644
+
69
−
0
View file @
e20e0226
package
field
import
(
"context"
"reflect"
"testing"
)
func
TestTimestamp_Decode
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
field
*
Field
data
interface
{}
want
interface
{}
wantErr
bool
}{
{
"Correct"
,
Timestamp
(),
int64
(
2
),
int64
(
2
),
false
},
// #0
{
"Correct"
,
Timestamp
(),
uint64
(
2
),
int64
(
2
),
false
},
// #1
{
"Correct"
,
Timestamp
(),
"13:10:44"
,
int64
(
47444
),
false
},
// #2
{
"Wrong data"
,
Timestamp
(),
""
,
nil
,
true
},
// #0
{
"Wrong data"
,
Timestamp
(),
[]
byte
(
""
),
nil
,
true
},
// #1
{
"Wrong data"
,
Timestamp
(),
2.2
,
nil
,
true
},
// #2
{
"Wrong data"
,
Timestamp
(),
"13:10"
,
nil
,
true
},
// #3
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
got
,
err
:=
Decode
(
context
.
Background
(),
tt
.
field
,
tt
.
data
)
if
(
err
!=
nil
)
!=
tt
.
wantErr
{
t
.
Errorf
(
"Decode() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
return
}
if
!
reflect
.
DeepEqual
(
got
,
tt
.
want
)
{
t
.
Errorf
(
"Decode() got = %v, want %v"
,
got
,
tt
.
want
)
}
})
}
}
func
TestTimestamp_Encode
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
field
*
Field
data
interface
{}
want
interface
{}
wantErr
bool
}{
{
"Correct"
,
Timestamp
(),
int64
(
2
),
int64
(
2
),
false
},
// #0
{
"Wrong data"
,
Timestamp
(),
""
,
nil
,
true
},
// #0
{
"Wrong data"
,
Timestamp
(),
[]
byte
(
""
),
nil
,
true
},
// #1
{
"Wrong data"
,
Timestamp
(),
2.2
,
nil
,
true
},
// #2
{
"Wrong data"
,
Timestamp
(),
uint64
(
2
),
nil
,
true
},
// #2
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
got
,
err
:=
Encode
(
context
.
Background
(),
tt
.
field
,
tt
.
data
)
if
(
err
!=
nil
)
!=
tt
.
wantErr
{
t
.
Errorf
(
"Decode() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
return
}
if
!
reflect
.
DeepEqual
(
got
,
tt
.
want
)
{
t
.
Errorf
(
"Decode() got = %v, want %v"
,
got
,
tt
.
want
)
}
})
}
}
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