blobref: enforce Go1.1 is required,

also types: rm Go1.0 RFC3339Nanos work-around

Change-Id: I9ce8495927fe349e7882f0a7d6c4f0051693fa2c
This commit is contained in:
mpl 2013-06-04 19:05:30 +02:00
parent d8e8674582
commit 481bdf346a
2 changed files with 13 additions and 41 deletions

View File

@ -0,0 +1,5 @@
// +build !go1.1
package blobref
Sorry, Camlistore requires Go 1.1.

View File

@ -21,13 +21,11 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"runtime" "runtime"
"strings"
"time" "time"
) )
var ( var (
goVersion = runtime.Version() goVersion = runtime.Version()
isGo10 = goVersion == "go1" || strings.HasPrefix(runtime.Version(), "go1.0")
dotNumbers = regexp.MustCompile(`\.\d+`) dotNumbers = regexp.MustCompile(`\.\d+`)
) )
@ -40,7 +38,6 @@ var (
_ json.Unmarshaler = (*Time3339)(nil) _ json.Unmarshaler = (*Time3339)(nil)
) )
func (t Time3339) String() string { func (t Time3339) String() string {
return time.Time(t).UTC().Format(time.RFC3339Nano) return time.Time(t).UTC().Format(time.RFC3339Nano)
} }
@ -49,65 +46,35 @@ func (t Time3339) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String()) return json.Marshal(t.String())
} }
func parseForGo10(s string) (time.Time, error) {
var numbers string
noNanos := dotNumbers.ReplaceAllStringFunc(s, func(m string) string {
numbers = m
return ""
})
t, err := time.Parse(time.RFC3339, noNanos)
if err != nil {
return t, fmt.Errorf("Failed to parse %q as an RFC 3339 time: %v", noNanos, err)
}
if numbers != "" {
nanos, err := time.ParseDuration(numbers + "s")
if err != nil {
return t, fmt.Errorf("Failed to parse %q as a duration: %v", numbers+"s", err)
}
t = t.Add(nanos)
}
return t, nil
}
func (t *Time3339) UnmarshalJSON(b []byte) error { func (t *Time3339) UnmarshalJSON(b []byte) error {
if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' { if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
return fmt.Errorf("types: failed to unmarshal non-string value %q as an RFC 3339 time") return fmt.Errorf("types: failed to unmarshal non-string value %q as an RFC 3339 time")
} }
tm, err := ParseTime3339(string(b[1 : len(b)-1])) tm, err := time.Parse(time.RFC3339Nano, string(b[1:len(b)-1]))
if err != nil { if err != nil {
return err return err
} }
*t = tm *t = Time3339(tm)
return nil return nil
} }
// ParseTime3339 parses a string in RFC3339 format. // ParseTime3339OrZero parses a string in RFC3339 format. If it's invalid,
func ParseTime3339(v string) (t Time3339, err error) {
var tm time.Time
if isGo10 {
tm, err = parseForGo10(v)
} else {
tm, err = time.Parse(time.RFC3339Nano, v)
}
return Time3339(tm), err
}
// ParseTime3339 parses a string in RFC3339 format. If it's invalid,
// the zero time value is returned instead. // the zero time value is returned instead.
func ParseTime3339OrZero(v string) Time3339 { func ParseTime3339OrZero(v string) Time3339 {
t, err := ParseTime3339(v) t, err := time.Parse(time.RFC3339Nano, v)
if err != nil { if err != nil {
return Time3339{} return Time3339{}
} }
return t return Time3339(t)
} }
func ParseTime3339OrZil(v string) *Time3339 { func ParseTime3339OrZil(v string) *Time3339 {
t, err := ParseTime3339(v) t, err := time.Parse(time.RFC3339Nano, v)
if err != nil { if err != nil {
return nil return nil
} }
return &t tm := Time3339(t)
return &tm
} }
// Time returns the time as a time.Time with slightly less stutter // Time returns the time as a time.Time with slightly less stutter