+
Skip to content

Add TOML unmarshal implementation #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions bytes.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package units

import (
"errors"
)

// Base2Bytes is the old non-SI power-of-2 byte scale (1024 bytes in a kilobyte,
// etc.).
type Base2Bytes int64
Expand All @@ -25,6 +29,8 @@ var (
oldBytesUnitMap = MakeUnitMap("B", "B", 1024)
)

var ErrIncorrectType = errors.New("not a string or int64")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be int, int8, int16, int32 as well

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be only int64 or string.

TOML spec says that all ints are int64:

64 bit (signed long) range expected (−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).

https://github.com/toml-lang/toml#user-content-integer

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be you should rename error? it is toml specific incorrect type error, but not package-wide incorrect type error.


// ParseBase2Bytes supports both iB and B in base-2 multipliers. That is, KB
// and KiB are both 1024.
// However "kB", which is the correct SI spelling of 1000 Bytes, is rejected.
Expand All @@ -40,6 +46,24 @@ func (b Base2Bytes) String() string {
return ToString(int64(b), 1024, "iB", "B")
}

// UnmarshalTOML implements TOML unmarshal interface.
func (b *Base2Bytes) UnmarshalTOML(value interface{}) error {
switch v := value.(type) {
case string:
var err error
*b, err = ParseBase2Bytes(v)
if err != nil {
return err
}
case int64:
*b = Base2Bytes(v)
default:
return ErrIncorrectType
}

return nil
}

var (
metricBytesUnitMap = MakeUnitMap("B", "B", 1000)
)
Expand Down
21 changes: 21 additions & 0 deletions bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,24 @@ func TestParseStrictBytes(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 1500000, int(n))
}

func TestUnmarshalTOML(t *testing.T) {
b := new(Base2Bytes)

assert.NoError(t, b.UnmarshalTOML("1KB1B"))
assert.Equal(t, 1025, int(*b))

assert.NoError(t, b.UnmarshalTOML(int64(1000)))
assert.Equal(t, 1000, int(*b))

assert.NoError(t, b.UnmarshalTOML(int64(1001)))
assert.Equal(t, 1001, int(*b))

assert.NoError(t, b.UnmarshalTOML(int64(-10102311)))
assert.Equal(t, -10102311, int(*b))

assert.NoError(t, b.UnmarshalTOML(int64(0)))
assert.Equal(t, 0, int(*b))

assert.Error(t, ErrIncorrectType, b.UnmarshalTOML(uint64(1001)))
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载