update vendor

This commit is contained in:
Qiu Jian
2019-09-17 23:59:39 +08:00
parent 2f516ee506
commit a7ebd7434a
6 changed files with 232 additions and 11 deletions
+1 -1
View File
@@ -151,7 +151,7 @@ require (
sigs.k8s.io/yaml v1.1.0 // indirect
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051
yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d
yunion.io/x/pkg v0.0.0-20190902093114-59ba154a6861
yunion.io/x/pkg v0.0.0-20190917154624-e89986e4e4d8
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e
yunion.io/x/sqlchemy v0.0.0-20190823062008-bb710661356f
yunion.io/x/structarg v0.0.0-20190809075558-115bed041de3
+2 -2
View File
@@ -583,8 +583,8 @@ yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d h1:59zrDL7Ft+hDukguJRmLr/Gdu/
yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d/go.mod h1:LC6f/4FozL0iaAbnFt2eDX9jlsyo3WiOUPm03d7+U4U=
yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf h1:OsKC+2ghZHwp+Ztm/MwKlLKKRiE7QcPG8eTp0GmsHbg=
yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
yunion.io/x/pkg v0.0.0-20190902093114-59ba154a6861 h1:4TDb45M/HGZ3fWvts253g+U3Rb80jgEiNf8R8Ex7HjQ=
yunion.io/x/pkg v0.0.0-20190902093114-59ba154a6861/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
yunion.io/x/pkg v0.0.0-20190917154624-e89986e4e4d8 h1:wutgIFVln8xNRH9WvrzINQuT1feByaUp0abXq9b7eL4=
yunion.io/x/pkg v0.0.0-20190917154624-e89986e4e4d8/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo=
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo=
yunion.io/x/sqlchemy v0.0.0-20190823062008-bb710661356f h1:maHGG78d6vLAyT/lGSOOsXWrylBfjdu+NMCGXFhLuhA=
+1 -1
View File
@@ -768,7 +768,7 @@ yunion.io/x/jsonutils
# yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d
yunion.io/x/log
yunion.io/x/log/hooks
# yunion.io/x/pkg v0.0.0-20190902093114-59ba154a6861
# yunion.io/x/pkg v0.0.0-20190917154624-e89986e4e4d8
yunion.io/x/pkg/util/version
yunion.io/x/pkg/utils
yunion.io/x/pkg/util/regutils
+199
View File
@@ -0,0 +1,199 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package errors
import (
"errors"
"fmt"
)
// MessageCountMap contains occurance for each error message.
type MessageCountMap map[string]int
// Aggregate represents an object that contains multiple errors, but does not
// necessarily have singular semantic meaning.
type Aggregate interface {
error
Errors() []error
}
// NewAggregate converts a slice of errors into an Aggregate interface, which
// is itself an implementation of the error interface. If the slice is empty,
// this returns nil.
// It will check if any of the element of input error list is nil, to avoid
// nil pointer panic when call Error().
func NewAggregate(errlist []error) Aggregate {
if len(errlist) == 0 {
return nil
}
// In case of input error list contains nil
var errs []error
for _, e := range errlist {
if e != nil {
errs = append(errs, e)
}
}
if len(errs) == 0 {
return nil
}
return aggregate(errs)
}
// This helper implements the error and Errors interfaces. Keeping it private
// prevents people from making an aggregate of 0 errors, which is not
// an error, but does satisfy the error interface.
type aggregate []error
// Error is part of the error interface.
func (agg aggregate) Error() string {
if len(agg) == 0 {
// This should never happen, really.
return ""
}
if len(agg) == 1 {
return agg[0].Error()
}
result := fmt.Sprintf("[%s", agg[0].Error())
for i := 1; i < len(agg); i++ {
result += fmt.Sprintf(", %s", agg[i].Error())
}
result += "]"
return result
}
// Errors is part of the Aggregate interface.
func (agg aggregate) Errors() []error {
return []error(agg)
}
// Matcher is used to match errors. Returns true if the error matches.
type Matcher func(error) bool
// FilterOut removes all errors that match any of the matchers from the input
// error. If the input is a singular error, only that error is tested. If the
// input implements the Aggregate interface, the list of errors will be
// processed recursively.
//
// This can be used, for example, to remove known-OK errors (such as io.EOF or
// os.PathNotFound) from a list of errors.
func FilterOut(err error, fns ...Matcher) error {
if err == nil {
return nil
}
if agg, ok := err.(Aggregate); ok {
return NewAggregate(filterErrors(agg.Errors(), fns...))
}
if !matchesError(err, fns...) {
return err
}
return nil
}
// matchesError returns true if any Matcher returns true
func matchesError(err error, fns ...Matcher) bool {
for _, fn := range fns {
if fn(err) {
return true
}
}
return false
}
// filterErrors returns any errors (or nested errors, if the list contains
// nested Errors) for which all fns return false. If no errors
// remain a nil list is returned. The resulting silec will have all
// nested slices flattened as a side effect.
func filterErrors(list []error, fns ...Matcher) []error {
result := []error{}
for _, err := range list {
r := FilterOut(err, fns...)
if r != nil {
result = append(result, r)
}
}
return result
}
// Flatten takes an Aggregate, which may hold other Aggregates in arbitrary
// nesting, and flattens them all into a single Aggregate, recursively.
func Flatten(agg Aggregate) Aggregate {
result := []error{}
if agg == nil {
return nil
}
for _, err := range agg.Errors() {
if a, ok := err.(Aggregate); ok {
r := Flatten(a)
if r != nil {
result = append(result, r.Errors()...)
}
} else {
if err != nil {
result = append(result, err)
}
}
}
return NewAggregate(result)
}
// CreateAggregateFromMessageCountMap converts MessageCountMap Aggregate
func CreateAggregateFromMessageCountMap(m MessageCountMap) Aggregate {
if m == nil {
return nil
}
result := make([]error, 0, len(m))
for errStr, count := range m {
var countStr string
if count > 1 {
countStr = fmt.Sprintf(" (repeated %v times)", count)
}
result = append(result, fmt.Errorf("%v%v", errStr, countStr))
}
return NewAggregate(result)
}
// Reduce will return err or, if err is an Aggregate and only has one item,
// the first item in the aggregate.
func Reduce(err error) error {
if agg, ok := err.(Aggregate); ok && err != nil {
switch len(agg.Errors()) {
case 1:
return agg.Errors()[0]
case 0:
return nil
}
}
return err
}
// AggregateGoroutines runs the provided functions in parallel, stuffing all
// non-nil errors into the returned Aggregate.
// Returns nil if all the functions complete successfully.
func AggregateGoroutines(funcs ...func() error) Aggregate {
errChan := make(chan error, len(funcs))
for _, f := range funcs {
go func(f func() error) { errChan <- f() }(f)
}
errs := make([]error, 0)
for i := 0; i < cap(errChan); i++ {
if err := <-errChan; err != nil {
errs = append(errs, err)
}
}
return NewAggregate(errs)
}
// ErrPreconditionViolated is returned when the precondition is violated
var ErrPreconditionViolated = errors.New("precondition is violated")
+3 -3
View File
@@ -65,9 +65,9 @@ func init() {
MONTH_REG = regexp.MustCompile(`^\d{4}-\d{2}$`)
DATE_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)
DATE_COMPACT_REG = regexp.MustCompile(`^\d{8}$`)
ISO_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$`)
ISO_NO_SECOND_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}Z$`)
FULLISO_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z$`)
ISO_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|\+\d{2}:\d{2})$`)
ISO_NO_SECOND_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(Z|\+\d{2}:\d{2})$`)
FULLISO_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3,9}(Z|\+\d{2}:\d{2})$`)
COMPACT_TIME_REG = regexp.MustCompile(`^\d{14}$`)
ZSTACK_TIME_REG = regexp.MustCompile(`^\w+ \d{1,2}, \d{4} \d{1,2}:\d{1,2}:\d{1,2} (AM|PM)$`) //ZStack time format "Apr 1, 2019 3:23:17 PM"
MYSQL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`)
+26 -4
View File
@@ -17,6 +17,7 @@ package timeutils
import (
"fmt"
"time"
"strings"
"yunion.io/x/pkg/util/regutils"
)
@@ -42,9 +43,10 @@ func Localify(now time.Time) time.Time {
}
const (
IsoTimeFormat = "2006-01-02T15:04:05Z"
IsoNoSecondTimeFormat = "2006-01-02T15:04Z"
FullIsoTimeFormat = "2006-01-02T15:04:05.000000Z"
IsoTimeFormat = "2006-01-02T15:04:05Z07:00"
IsoNoSecondTimeFormat = "2006-01-02T15:04Z07:00"
FullIsoTimeFormat = "2006-01-02T15:04:05.000000Z07:00"
FullIsoNanoTimeFormat = "2006-01-02T15:04:05.000000000Z07:00"
MysqlTimeFormat = "2006-01-02 15:04:05"
NormalTimeFormat = "2006-01-02T15:04:05"
FullNormalTimeFormat = "2006-01-02T15:04:05.000000"
@@ -68,6 +70,10 @@ func FullIsoTime(now time.Time) string {
return Utcify(now).Format(FullIsoTimeFormat)
}
func FullIsoNanoTime(now time.Time) string {
return Utcify(now).Format(FullIsoNanoTimeFormat)
}
func MysqlTime(now time.Time) string {
return Utcify(now).Format(MysqlTimeFormat)
}
@@ -100,8 +106,23 @@ func ParseIsoNoSecondTime(str string) (time.Time, error) {
return time.Parse(IsoNoSecondTimeFormat, str)
}
func toFullIsoNanoTimeFormat(str string) string {
// 2019-09-17T20:50:17.66667134+08:00
subsecStr := str[20:]
pos := strings.IndexByte(subsecStr, 'Z')
if pos < 0 {
pos = strings.IndexByte(subsecStr, '+')
}
leftOver := subsecStr[pos:]
subsecStr = subsecStr[:pos]
for len(subsecStr) < 9 {
subsecStr += "0"
}
return str[:20] + subsecStr + leftOver
}
func ParseFullIsoTime(str string) (time.Time, error) {
return time.Parse(FullIsoTimeFormat, str)
return time.Parse(FullIsoNanoTimeFormat, toFullIsoNanoTimeFormat(str))
}
func ParseMysqlTime(str string) (time.Time, error) {
@@ -137,6 +158,7 @@ func ParseZStackDate(str string) (time.Time, error) {
}
func ParseTimeStr(str string) (time.Time, error) {
str = strings.TrimSpace(str)
if regutils.MatchFullISOTime(str) {
return ParseFullIsoTime(str)
} else if regutils.MatchISOTime(str) {