Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
time.Duration support
Browse files Browse the repository at this point in the history
  • Loading branch information
shixzie committed Jun 27, 2017
1 parent 4652698 commit 6482975
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ uint uint8 uint16 uint32 uint64
float32 float64
string
time.Time
time.Duration
```

## Installation
Expand Down
6 changes: 6 additions & 0 deletions nlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ func (nl *NL) RegisterModel(i interface{}, samples []string, ops ...ModelOption)
if v, ok := val.Field(i).Interface().(time.Time); ok {
mod.fields = append(mod.fields, field{i, tpy.Field(i).Name, v})
continue NextField
} else if v, ok := val.Field(i).Interface().(time.Duration); ok {
mod.fields = append(mod.fields, field{i, tpy.Field(i).Name, v})
continue NextField
}
switch val.Field(i).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.String:
Expand Down Expand Up @@ -305,6 +308,9 @@ func (m *model) fit(expr string) interface{} {
case time.Time:
v, _ := time.ParseInLocation(m.timeFormat, e.value, m.timeLocation)
val.Elem().Field(e.field.index).Set(reflect.ValueOf(v))
case time.Duration:
v, _ := time.ParseDuration(e.value)
val.Elem().Field(e.field.index).Set(reflect.ValueOf(v))
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions nlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestNL_P(t *testing.T) {
Uint uint
Float float32
Time time.Time
Dur time.Duration
}

tSamples := []string{
Expand All @@ -24,6 +25,7 @@ func TestNL_P(t *testing.T) {
"uint {Uint}",
"float {Float}",
"time {Time}",
"dur {Dur}",
"string {String} int {Int}",
"string {String} time {Time}",
}
Expand All @@ -45,6 +47,11 @@ func TestNL_P(t *testing.T) {
t.Error(err)
}

dur, err := time.ParseDuration("4h2m")
if err != nil {
t.Error(err)
}

cases := []struct {
expression string
want interface{}
Expand All @@ -69,6 +76,17 @@ func TestNL_P(t *testing.T) {
"time 05-18-1999_6:42pm",
tim,
},
{
"dur 4h2m",
dur,
},
{
"string Lmao int 42",
&T{
String: "Lmao",
Int: 42,
},
},
{
"string What's Up Boy time 05-18-1999_6:42pm",
&T{
Expand Down Expand Up @@ -101,6 +119,10 @@ func TestNL_P(t *testing.T) {
if !c.Time.Equal(v) {
t.Errorf("test#%d: got %v want %v", i, c.Time, v)
}
case time.Duration:
if c.Dur != v {
t.Errorf("test#%d: got %v want %v", i, c.Dur, v)
}
case *T:
if !reflect.DeepEqual(c, v) {
t.Errorf("test#%d: got %v want %v", i, c, v)
Expand Down

0 comments on commit 6482975

Please sign in to comment.