podman
211 строк · 4.9 Кб
1// Copyright 2015 go-swagger maintainers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package strfmt16
17import (18"database/sql/driver"19"encoding/json"20"errors"21"fmt"22"regexp"23"strconv"24"strings"25"time"26
27"go.mongodb.org/mongo-driver/bson"28)
29
30func init() {31d := Duration(0)32// register this format in the default registry33Default.Add("duration", &d, IsDuration)34}
35
36var (37timeUnits = [][]string{38{"ns", "nano"},39{"us", "µs", "micro"},40{"ms", "milli"},41{"s", "sec"},42{"m", "min"},43{"h", "hr", "hour"},44{"d", "day"},45{"w", "wk", "week"},46}47
48timeMultiplier = map[string]time.Duration{49"ns": time.Nanosecond,50"us": time.Microsecond,51"ms": time.Millisecond,52"s": time.Second,53"m": time.Minute,54"h": time.Hour,55"d": 24 * time.Hour,56"w": 7 * 24 * time.Hour,57}58
59durationMatcher = regexp.MustCompile(`((\d+)\s*([A-Za-zµ]+))`)60)
61
62// IsDuration returns true if the provided string is a valid duration
63func IsDuration(str string) bool {64_, err := ParseDuration(str)65return err == nil66}
67
68// Duration represents a duration
69//
70// Duration stores a period of time as a nanosecond count, with the largest
71// repesentable duration being approximately 290 years.
72//
73// swagger:strfmt duration
74type Duration time.Duration75
76// MarshalText turns this instance into text
77func (d Duration) MarshalText() ([]byte, error) {78return []byte(time.Duration(d).String()), nil79}
80
81// UnmarshalText hydrates this instance from text
82func (d *Duration) UnmarshalText(data []byte) error { // validation is performed later on83dd, err := ParseDuration(string(data))84if err != nil {85return err86}87*d = Duration(dd)88return nil89}
90
91// ParseDuration parses a duration from a string, compatible with scala duration syntax
92func ParseDuration(cand string) (time.Duration, error) {93if dur, err := time.ParseDuration(cand); err == nil {94return dur, nil95}96
97var dur time.Duration98ok := false99for _, match := range durationMatcher.FindAllStringSubmatch(cand, -1) {100
101factor, err := strconv.Atoi(match[2]) // converts string to int102if err != nil {103return 0, err104}105unit := strings.ToLower(strings.TrimSpace(match[3]))106
107for _, variants := range timeUnits {108last := len(variants) - 1109multiplier := timeMultiplier[variants[0]]110
111for i, variant := range variants {112if (last == i && strings.HasPrefix(unit, variant)) || strings.EqualFold(variant, unit) {113ok = true114dur += (time.Duration(factor) * multiplier)115}116}117}118}119
120if ok {121return dur, nil122}123return 0, fmt.Errorf("unable to parse %s as duration", cand)124}
125
126// Scan reads a Duration value from database driver type.
127func (d *Duration) Scan(raw interface{}) error {128switch v := raw.(type) {129// TODO: case []byte: // ?130case int64:131*d = Duration(v)132case float64:133*d = Duration(int64(v))134case nil:135*d = Duration(0)136default:137return fmt.Errorf("cannot sql.Scan() strfmt.Duration from: %#v", v)138}139
140return nil141}
142
143// Value converts Duration to a primitive value ready to be written to a database.
144func (d Duration) Value() (driver.Value, error) {145return driver.Value(int64(d)), nil146}
147
148// String converts this duration to a string
149func (d Duration) String() string {150return time.Duration(d).String()151}
152
153// MarshalJSON returns the Duration as JSON
154func (d Duration) MarshalJSON() ([]byte, error) {155return json.Marshal(time.Duration(d).String())156}
157
158// UnmarshalJSON sets the Duration from JSON
159func (d *Duration) UnmarshalJSON(data []byte) error {160if string(data) == jsonNull {161return nil162}163
164var dstr string165if err := json.Unmarshal(data, &dstr); err != nil {166return err167}168tt, err := ParseDuration(dstr)169if err != nil {170return err171}172*d = Duration(tt)173return nil174}
175
176func (d Duration) MarshalBSON() ([]byte, error) {177return bson.Marshal(bson.M{"data": d.String()})178}
179
180func (d *Duration) UnmarshalBSON(data []byte) error {181var m bson.M182if err := bson.Unmarshal(data, &m); err != nil {183return err184}185
186if data, ok := m["data"].(string); ok {187rd, err := ParseDuration(data)188if err != nil {189return err190}191*d = Duration(rd)192return nil193}194
195return errors.New("couldn't unmarshal bson bytes value as Date")196}
197
198// DeepCopyInto copies the receiver and writes its value into out.
199func (d *Duration) DeepCopyInto(out *Duration) {200*out = *d201}
202
203// DeepCopy copies the receiver into a new Duration.
204func (d *Duration) DeepCopy() *Duration {205if d == nil {206return nil207}208out := new(Duration)209d.DeepCopyInto(out)210return out211}
212