-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: jkoberg <[email protected]>
- Loading branch information
Showing
3 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2018-2022 CERN | ||
// | ||
// 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. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
// Package bytesize provides easy conversions from human readable strings (eg. 10MB) to bytes | ||
package bytesize | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// ByteSize is the size in bytes | ||
type ByteSize uint64 | ||
|
||
// List of available byte sizes | ||
// NOTE: max is exabyte as we convert to uint64 | ||
const ( | ||
KB ByteSize = 1000 | ||
MB ByteSize = 1000 * KB | ||
GB ByteSize = 1000 * MB | ||
TB ByteSize = 1000 * GB | ||
PB ByteSize = 1000 * TB | ||
EB ByteSize = 1000 * PB | ||
|
||
KiB ByteSize = 1024 | ||
MiB ByteSize = 1024 * KiB | ||
GiB ByteSize = 1024 * MiB | ||
TiB ByteSize = 1024 * GiB | ||
PiB ByteSize = 1024 * TiB | ||
EiB ByteSize = 1024 * PiB | ||
) | ||
|
||
// Parse parses a Bytesize from a string | ||
func Parse(s string) (ByteSize, error) { | ||
sanitized := strings.TrimSpace(s) | ||
if !strings.HasSuffix(sanitized, "B") { | ||
u, err := strconv.Atoi(sanitized) | ||
return ByteSize(u), err | ||
} | ||
|
||
var ( | ||
value int | ||
unit string | ||
) | ||
|
||
template := "%d%s" | ||
_, err := fmt.Sscanf(sanitized, template, &value, &unit) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
bytes := ByteSize(value) | ||
switch unit { | ||
case "KB": | ||
bytes *= KB | ||
case "MB": | ||
bytes *= MB | ||
case "GB": | ||
bytes *= GB | ||
case "TB": | ||
bytes *= TB | ||
case "PB": | ||
bytes *= PB | ||
case "EB": | ||
bytes *= EB | ||
case "KiB": | ||
bytes *= KiB | ||
case "MiB": | ||
bytes *= MiB | ||
case "GiB": | ||
bytes *= GiB | ||
case "TiB": | ||
bytes *= TiB | ||
case "PiB": | ||
bytes *= PiB | ||
case "EiB": | ||
bytes *= EiB | ||
default: | ||
return 0, fmt.Errorf("unknown unit '%s'. Use common abbreviations such as KB, MiB, GB", unit) | ||
} | ||
|
||
return bytes, nil | ||
} | ||
|
||
// Bytes converts the ByteSize to an uint64 | ||
func (b ByteSize) Bytes() uint64 { | ||
return uint64(b) | ||
} | ||
|
||
// String converts the ByteSize to a string | ||
func (b ByteSize) String() string { | ||
return strconv.FormatUint(uint64(b), 10) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright 2018-2022 CERN | ||
// | ||
// 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. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package bytesize_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cs3org/reva/v2/pkg/bytesize" | ||
"github.com/test-go/testify/require" | ||
) | ||
|
||
func TestParseSpecial(t *testing.T) { | ||
testCases := []struct { | ||
Alias string | ||
Input string | ||
ExpectedOutput uint64 | ||
ExpectError bool | ||
}{ | ||
{ | ||
Alias: "it assumes bytes", | ||
Input: "100", | ||
ExpectedOutput: 100, | ||
}, | ||
{ | ||
Alias: "it accepts a space between value and unit", | ||
Input: "1 MB", | ||
ExpectedOutput: 1000000, | ||
}, | ||
{ | ||
Alias: "it accepts also more spaces between value and unit", | ||
Input: "1 MB", | ||
ExpectedOutput: 1000000, | ||
}, | ||
{ | ||
Alias: "it ignores trailing and leading spaces", | ||
Input: " 1MB ", | ||
ExpectedOutput: 1000000, | ||
}, | ||
{ | ||
Alias: "it errors on unknown units", | ||
Input: "1SB", | ||
ExpectedOutput: 0, | ||
ExpectError: true, | ||
}, | ||
{ | ||
Alias: "it multiplies correctly", | ||
Input: "16MB", | ||
ExpectedOutput: 16000000, | ||
}, | ||
{ | ||
Alias: "it errors when no value is given", | ||
Input: "GB", | ||
ExpectedOutput: 0, | ||
ExpectError: true, | ||
}, | ||
{ | ||
Alias: "it errors when bad input is given", | ||
Input: ",as!@@delta", | ||
ExpectedOutput: 0, | ||
ExpectError: true, | ||
}, | ||
{ | ||
Alias: "it errors when using floats", | ||
Input: "1.024GB", | ||
ExpectedOutput: 0, | ||
ExpectError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
actual, err := bytesize.Parse(tc.Input) | ||
if tc.ExpectError { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
|
||
require.Equal(t, tc.ExpectError, err != nil, tc.Alias) | ||
require.Equal(t, int(tc.ExpectedOutput), int(actual), tc.Alias) | ||
} | ||
} | ||
|
||
func TestParseHappy(t *testing.T) { | ||
testCases := []struct { | ||
Input string | ||
Expected uint64 | ||
}{ | ||
{Input: "1", Expected: 1}, | ||
{Input: "1KB", Expected: 1000}, | ||
{Input: "1MB", Expected: 1000000}, | ||
{Input: "1GB", Expected: 1000000000}, | ||
{Input: "1TB", Expected: 1000000000000}, | ||
{Input: "1PB", Expected: 1000000000000000}, | ||
{Input: "1EB", Expected: 1000000000000000000}, | ||
{Input: "1MiB", Expected: 1048576}, | ||
{Input: "1GiB", Expected: 1073741824}, | ||
{Input: "1TiB", Expected: 1099511627776}, | ||
{Input: "1PiB", Expected: 1125899906842624}, | ||
{Input: "1EiB", Expected: 1152921504606846976}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
actual, err := bytesize.Parse(tc.Input) | ||
require.NoError(t, err) | ||
require.Equal(t, int(tc.Expected), int(actual), fmt.Sprintf("case %s", tc.Input)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters