Skip to content

Commit

Permalink
Merge pull request #25 from LUSHDigital/feature/nulltime-fix
Browse files Browse the repository at this point in the history
fixed nulltime unmarshalling into zero time utc
  • Loading branch information
codingconcepts authored Jul 6, 2018
2 parents c159861 + b7ff73a commit df8d455
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
7 changes: 4 additions & 3 deletions init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ CREATE TABLE `common_cases` (
`not_null_int` int(11) NOT NULL,
`null_string` int(11) DEFAULT NULL,
`null_int` int(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` int(11) DEFAULT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Expand All @@ -21,5 +21,6 @@ CREATE TABLE `complex_cases` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`raw_json` json NOT NULL,
`size_enum` enum('X-SMALL','SMALL','MEDIUM','LARGE','X-LARGE') DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
40 changes: 25 additions & 15 deletions tmpl/x_helpers.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,21 +271,31 @@

// UnmarshalJSON for NullTime
func (n *NullTime) UnmarshalJSON(b []byte) error {
s := string(b)
s = strings.Trim(s, `"`)

var tim time.Time
if !strings.EqualFold(s, "null") {
var err error
if tim, err = time.Parse(time.RFC3339, s); err != nil {
n.Valid = false
return err
}
}

n.Time = tim
n.Valid = true
return nil
s := string(b)
s = strings.Trim(s, `"`)

var (
zeroTime time.Time
tim time.Time
err error
)

if strings.EqualFold(s, "null") {
return nil
}

if tim, err = time.Parse(time.RFC3339, s); err != nil {
n.Valid = false
return err
}

if tim == zeroTime {
return nil
}

n.Time = tim
n.Valid = true
return nil
}

// Scan for NullTime
Expand Down

0 comments on commit df8d455

Please sign in to comment.