Merge pull request #8894 from gyuho/a

vendor: upgrade grpc-gateway to v1.3.0, dustin/go-humanize
release-3.3
Gyu-Ho Lee 2017-11-17 15:27:51 -08:00 committed by GitHub
commit 3cff8dd6f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 148 additions and 54 deletions

View File

@ -113,7 +113,7 @@ func humanateBigBytes(s, base *big.Int, sizes []string) string {
// //
// See also: ParseBigBytes. // See also: ParseBigBytes.
// //
// BigBytes(82854982) -> 83MB // BigBytes(82854982) -> 83 MB
func BigBytes(s *big.Int) string { func BigBytes(s *big.Int) string {
sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
return humanateBigBytes(s, bigSIExp, sizes) return humanateBigBytes(s, bigSIExp, sizes)
@ -123,7 +123,7 @@ func BigBytes(s *big.Int) string {
// //
// See also: ParseBigBytes. // See also: ParseBigBytes.
// //
// BigIBytes(82854982) -> 79MiB // BigIBytes(82854982) -> 79 MiB
func BigIBytes(s *big.Int) string { func BigIBytes(s *big.Int) string {
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"} sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
return humanateBigBytes(s, bigIECExp, sizes) return humanateBigBytes(s, bigIECExp, sizes)
@ -134,19 +134,28 @@ func BigIBytes(s *big.Int) string {
// //
// See also: BigBytes, BigIBytes. // See also: BigBytes, BigIBytes.
// //
// ParseBigBytes("42MB") -> 42000000, nil // ParseBigBytes("42 MB") -> 42000000, nil
// ParseBigBytes("42mib") -> 44040192, nil // ParseBigBytes("42 mib") -> 44040192, nil
func ParseBigBytes(s string) (*big.Int, error) { func ParseBigBytes(s string) (*big.Int, error) {
lastDigit := 0 lastDigit := 0
hasComma := false
for _, r := range s { for _, r := range s {
if !(unicode.IsDigit(r) || r == '.') { if !(unicode.IsDigit(r) || r == '.' || r == ',') {
break break
} }
if r == ',' {
hasComma = true
}
lastDigit++ lastDigit++
} }
num := s[:lastDigit]
if hasComma {
num = strings.Replace(num, ",", "", -1)
}
val := &big.Rat{} val := &big.Rat{}
_, err := fmt.Sscanf(s[:lastDigit], "%f", val) _, err := fmt.Sscanf(num, "%f", val)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -84,7 +84,7 @@ func humanateBytes(s uint64, base float64, sizes []string) string {
// //
// See also: ParseBytes. // See also: ParseBytes.
// //
// Bytes(82854982) -> 83MB // Bytes(82854982) -> 83 MB
func Bytes(s uint64) string { func Bytes(s uint64) string {
sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"} sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
return humanateBytes(s, 1000, sizes) return humanateBytes(s, 1000, sizes)
@ -94,7 +94,7 @@ func Bytes(s uint64) string {
// //
// See also: ParseBytes. // See also: ParseBytes.
// //
// IBytes(82854982) -> 79MiB // IBytes(82854982) -> 79 MiB
func IBytes(s uint64) string { func IBytes(s uint64) string {
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"} sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
return humanateBytes(s, 1024, sizes) return humanateBytes(s, 1024, sizes)
@ -105,18 +105,27 @@ func IBytes(s uint64) string {
// //
// See Also: Bytes, IBytes. // See Also: Bytes, IBytes.
// //
// ParseBytes("42MB") -> 42000000, nil // ParseBytes("42 MB") -> 42000000, nil
// ParseBytes("42mib") -> 44040192, nil // ParseBytes("42 mib") -> 44040192, nil
func ParseBytes(s string) (uint64, error) { func ParseBytes(s string) (uint64, error) {
lastDigit := 0 lastDigit := 0
hasComma := false
for _, r := range s { for _, r := range s {
if !(unicode.IsDigit(r) || r == '.') { if !(unicode.IsDigit(r) || r == '.' || r == ',') {
break break
} }
if r == ',' {
hasComma = true
}
lastDigit++ lastDigit++
} }
f, err := strconv.ParseFloat(s[:lastDigit], 64) num := s[:lastDigit]
if hasComma {
num = strings.Replace(num, ",", "", -1)
}
f, err := strconv.ParseFloat(num, 64)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -2,6 +2,7 @@ package humanize
import ( import (
"bytes" "bytes"
"math"
"math/big" "math/big"
"strconv" "strconv"
"strings" "strings"
@ -13,6 +14,12 @@ import (
// e.g. Comma(834142) -> 834,142 // e.g. Comma(834142) -> 834,142
func Comma(v int64) string { func Comma(v int64) string {
sign := "" sign := ""
// Min int64 can't be negated to a usable value, so it has to be special cased.
if v == math.MinInt64 {
return "-9,223,372,036,854,775,808"
}
if v < 0 { if v < 0 {
sign = "-" sign = "-"
v = 0 - v v = 0 - v
@ -39,7 +46,7 @@ func Comma(v int64) string {
// Commaf produces a string form of the given number in base 10 with // Commaf produces a string form of the given number in base 10 with
// commas after every three orders of magnitude. // commas after every three orders of magnitude.
// //
// e.g. Comma(834142.32) -> 834,142.32 // e.g. Commaf(834142.32) -> 834,142.32
func Commaf(v float64) string { func Commaf(v float64) string {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
if v < 0 { if v < 0 {

40
cmd/vendor/github.com/dustin/go-humanize/commaf.go generated vendored Normal file
View File

@ -0,0 +1,40 @@
// +build go1.6
package humanize
import (
"bytes"
"math/big"
"strings"
)
// BigCommaf produces a string form of the given big.Float in base 10
// with commas after every three orders of magnitude.
func BigCommaf(v *big.Float) string {
buf := &bytes.Buffer{}
if v.Sign() < 0 {
buf.Write([]byte{'-'})
v.Abs(v)
}
comma := []byte{','}
parts := strings.Split(v.Text('f', -1), ".")
pos := 0
if len(parts[0])%3 != 0 {
pos += len(parts[0]) % 3
buf.WriteString(parts[0][:pos])
buf.Write(comma)
}
for ; pos < len(parts[0]); pos += 3 {
buf.WriteString(parts[0][pos : pos+3])
buf.Write(comma)
}
buf.Truncate(buf.Len() - 1)
if len(parts) > 1 {
buf.Write([]byte{'.'})
buf.WriteString(parts[1])
}
return buf.String()
}

View File

@ -2,7 +2,7 @@
Package humanize converts boring ugly numbers to human-friendly strings and back. Package humanize converts boring ugly numbers to human-friendly strings and back.
Durations can be turned into strings such as "3 days ago", numbers Durations can be turned into strings such as "3 days ago", numbers
representing sizes like 82854982 into useful strings like, "83MB" or representing sizes like 82854982 into useful strings like, "83 MB" or
"79MiB" (whichever you prefer). "79 MiB" (whichever you prefer).
*/ */
package humanize package humanize

View File

@ -160,7 +160,7 @@ func FormatFloat(format string, n float64) string {
intf, fracf := math.Modf(n + renderFloatPrecisionRounders[precision]) intf, fracf := math.Modf(n + renderFloatPrecisionRounders[precision])
// generate integer part string // generate integer part string
intStr := strconv.Itoa(int(intf)) intStr := strconv.FormatInt(int64(intf), 10)
// add thousand separator if required // add thousand separator if required
if len(thousandStr) > 0 { if len(thousandStr) > 0 {

View File

@ -41,7 +41,7 @@ func revfmap(in map[float64]string) map[string]float64 {
var riParseRegex *regexp.Regexp var riParseRegex *regexp.Regexp
func init() { func init() {
ri := `^([0-9.]+)\s?([` ri := `^([\-0-9.]+)\s?([`
for _, v := range siPrefixTable { for _, v := range siPrefixTable {
ri += v ri += v
} }
@ -61,18 +61,21 @@ func ComputeSI(input float64) (float64, string) {
if input == 0 { if input == 0 {
return 0, "" return 0, ""
} }
exponent := math.Floor(logn(input, 10)) mag := math.Abs(input)
exponent := math.Floor(logn(mag, 10))
exponent = math.Floor(exponent/3) * 3 exponent = math.Floor(exponent/3) * 3
value := input / math.Pow(10, exponent) value := mag / math.Pow(10, exponent)
// Handle special case where value is exactly 1000.0 // Handle special case where value is exactly 1000.0
// Should return 1M instead of 1000k // Should return 1 M instead of 1000 k
if value == 1000.0 { if value == 1000.0 {
exponent += 3 exponent += 3
value = input / math.Pow(10, exponent) value = mag / math.Pow(10, exponent)
} }
value = math.Copysign(value, input)
prefix := siPrefixTable[exponent] prefix := siPrefixTable[exponent]
return value, prefix return value, prefix
} }
@ -83,8 +86,8 @@ func ComputeSI(input float64) (float64, string) {
// //
// See also: ComputeSI, ParseSI. // See also: ComputeSI, ParseSI.
// //
// e.g. SI(1000000, B) -> 1MB // e.g. SI(1000000, "B") -> 1 MB
// e.g. SI(2.2345e-12, "F") -> 2.2345pF // e.g. SI(2.2345e-12, "F") -> 2.2345 pF
func SI(input float64, unit string) string { func SI(input float64, unit string) string {
value, prefix := ComputeSI(input) value, prefix := ComputeSI(input)
return Ftoa(value) + " " + prefix + unit return Ftoa(value) + " " + prefix + unit
@ -96,7 +99,7 @@ var errInvalid = errors.New("invalid input")
// //
// See also: SI, ComputeSI. // See also: SI, ComputeSI.
// //
// e.g. ParseSI(2.2345pF) -> (2.2345e-12, "F", nil) // e.g. ParseSI("2.2345 pF") -> (2.2345e-12, "F", nil)
func ParseSI(input string) (float64, string, error) { func ParseSI(input string) (float64, string, error) {
found := riParseRegex.FindStringSubmatch(input) found := riParseRegex.FindStringSubmatch(input)
if len(found) != 4 { if len(found) != 4 {

View File

@ -9,9 +9,7 @@ import (
// Seconds-based time units // Seconds-based time units
const ( const (
Minute = 60 Day = 24 * time.Hour
Hour = 60 * Minute
Day = 24 * Hour
Week = 7 * Day Week = 7 * Day
Month = 30 * Day Month = 30 * Day
Year = 12 * Month Year = 12 * Month
@ -25,18 +23,35 @@ func Time(then time.Time) string {
return RelTime(then, time.Now(), "ago", "from now") return RelTime(then, time.Now(), "ago", "from now")
} }
var magnitudes = []struct { // A RelTimeMagnitude struct contains a relative time point at which
d int64 // the relative format of time will switch to a new format string. A
format string // slice of these in ascending order by their "D" field is passed to
divby int64 // CustomRelTime to format durations.
}{ //
{1, "now", 1}, // The Format field is a string that may contain a "%s" which will be
{2, "1 second %s", 1}, // replaced with the appropriate signed label (e.g. "ago" or "from
{Minute, "%d seconds %s", 1}, // now") and a "%d" that will be replaced by the quantity.
{2 * Minute, "1 minute %s", 1}, //
{Hour, "%d minutes %s", Minute}, // The DivBy field is the amount of time the time difference must be
{2 * Hour, "1 hour %s", 1}, // divided by in order to display correctly.
{Day, "%d hours %s", Hour}, //
// e.g. if D is 2*time.Minute and you want to display "%d minutes %s"
// DivBy should be time.Minute so whatever the duration is will be
// expressed in minutes.
type RelTimeMagnitude struct {
D time.Duration
Format string
DivBy time.Duration
}
var defaultMagnitudes = []RelTimeMagnitude{
{time.Second, "now", time.Second},
{2 * time.Second, "1 second %s", 1},
{time.Minute, "%d seconds %s", time.Second},
{2 * time.Minute, "1 minute %s", 1},
{time.Hour, "%d minutes %s", time.Minute},
{2 * time.Hour, "1 hour %s", 1},
{Day, "%d hours %s", time.Hour},
{2 * Day, "1 day %s", 1}, {2 * Day, "1 day %s", 1},
{Week, "%d days %s", Day}, {Week, "%d days %s", Day},
{2 * Week, "1 week %s", 1}, {2 * Week, "1 week %s", 1},
@ -57,35 +72,46 @@ var magnitudes = []struct {
// //
// RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier" // RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier"
func RelTime(a, b time.Time, albl, blbl string) string { func RelTime(a, b time.Time, albl, blbl string) string {
lbl := albl return CustomRelTime(a, b, albl, blbl, defaultMagnitudes)
diff := b.Unix() - a.Unix() }
after := a.After(b) // CustomRelTime formats a time into a relative string.
if after { //
// It takes two times two labels and a table of relative time formats.
// In addition to the generic time delta string (e.g. 5 minutes), the
// labels are used applied so that the label corresponding to the
// smaller time is applied.
func CustomRelTime(a, b time.Time, albl, blbl string, magnitudes []RelTimeMagnitude) string {
lbl := albl
diff := b.Sub(a)
if a.After(b) {
lbl = blbl lbl = blbl
diff = a.Unix() - b.Unix() diff = a.Sub(b)
} }
n := sort.Search(len(magnitudes), func(i int) bool { n := sort.Search(len(magnitudes), func(i int) bool {
return magnitudes[i].d > diff return magnitudes[i].D > diff
}) })
if n >= len(magnitudes) {
n = len(magnitudes) - 1
}
mag := magnitudes[n] mag := magnitudes[n]
args := []interface{}{} args := []interface{}{}
escaped := false escaped := false
for _, ch := range mag.format { for _, ch := range mag.Format {
if escaped { if escaped {
switch ch { switch ch {
case '%':
case 's': case 's':
args = append(args, lbl) args = append(args, lbl)
case 'd': case 'd':
args = append(args, diff/mag.divby) args = append(args, diff/mag.DivBy)
} }
escaped = false escaped = false
} else { } else {
escaped = ch == '%' escaped = ch == '%'
} }
} }
return fmt.Sprintf(mag.format, args...) return fmt.Sprintf(mag.Format, args...)
} }

6
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: 2244aad388c98d02a53083ff704bc2f5da72978ef7330ef588ced88822b70e23 hash: 8136b7373396e6f72a1f99838d706292c688ccb94bb1bddee7124babde335e31
updated: 2017-11-16T10:30:18.004311-08:00 updated: 2017-11-17T14:01:41.761462-08:00
imports: imports:
- name: github.com/beorn7/perks - name: github.com/beorn7/perks
version: 4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9 version: 4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9
@ -31,7 +31,7 @@ imports:
- name: github.com/dgrijalva/jwt-go - name: github.com/dgrijalva/jwt-go
version: d2709f9f1f31ebcda9651b03077758c1f3a0018c version: d2709f9f1f31ebcda9651b03077758c1f3a0018c
- name: github.com/dustin/go-humanize - name: github.com/dustin/go-humanize
version: 8929fe90cee4b2cb9deb468b51fb34eba64d1bf0 version: bb3d318650d48840a39aa21a027c6630e198e626
- name: github.com/ghodss/yaml - name: github.com/ghodss/yaml
version: 0ca9ea5df5451ffdf184b4428c902747c2c11cd7 version: 0ca9ea5df5451ffdf184b4428c902747c2c11cd7
- name: github.com/gogo/protobuf - name: github.com/gogo/protobuf

View File

@ -23,7 +23,7 @@ import:
- package: github.com/cpuguy83/go-md2man - package: github.com/cpuguy83/go-md2man
version: 23709d0847197db6021a51fdb193e66e9222d4e7 version: 23709d0847197db6021a51fdb193e66e9222d4e7
- package: github.com/dustin/go-humanize - package: github.com/dustin/go-humanize
version: 8929fe90cee4b2cb9deb468b51fb34eba64d1bf0 version: bb3d318650d48840a39aa21a027c6630e198e626
- package: github.com/ghodss/yaml - package: github.com/ghodss/yaml
version: v1.0.0 version: v1.0.0
- package: github.com/gogo/protobuf - package: github.com/gogo/protobuf
@ -44,7 +44,7 @@ import:
- package: github.com/google/btree - package: github.com/google/btree
version: 925471ac9e2131377a91e1595defec898166fe49 version: 925471ac9e2131377a91e1595defec898166fe49
- package: github.com/grpc-ecosystem/grpc-gateway - package: github.com/grpc-ecosystem/grpc-gateway
version: v1.3 version: v1.3.0
subpackages: subpackages:
- runtime - runtime
- runtime/internal - runtime/internal