-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] yaml semver marshal/unmarshal #41
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| package semver | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "encoding/json" | ||
| "fmt" | ||
| "regexp" | ||
|
|
@@ -229,6 +230,90 @@ func isNumeric(s string) (v uint16, ok bool) { | |
| // Serialization and Deserialization | ||
| //=========================================================================== | ||
|
|
||
| func (v Version) MarshalBinary() (data []byte, err error) { | ||
| pr := []byte(v.PreRelease) | ||
| bm := []byte(v.BuildMeta) | ||
|
|
||
| size := 5*binary.MaxVarintLen16 + len(pr) + len(bm) | ||
| data = make([]byte, size) | ||
|
|
||
| i := binary.PutUvarint(data, uint64(v.Major)) | ||
| i += binary.PutUvarint(data[i:], uint64(v.Minor)) | ||
| i += binary.PutUvarint(data[i:], uint64(v.Patch)) | ||
| i += binary.PutUvarint(data[i:], uint64(len(pr))) | ||
| i += copy(data[i:], pr) | ||
| i += binary.PutUvarint(data[i:], uint64(len(bm))) | ||
| i += copy(data[i:], bm) | ||
|
|
||
| data = data[:i:i] | ||
| return data, nil | ||
| } | ||
|
|
||
| func (v *Version) UnmarshalBinary(data []byte) error { | ||
| if len(data) < 5 { | ||
| return ErrDataSize | ||
| } | ||
|
|
||
| var ( | ||
| i, j int | ||
| c uint64 | ||
| ) | ||
|
|
||
| c, j = binary.Uvarint(data[i:]) | ||
| if j <= 0 { | ||
| return ErrDataSize | ||
| } | ||
| v.Major = uint16(c) | ||
|
|
||
| i += j | ||
| c, j = binary.Uvarint(data[i:]) | ||
| if j <= 0 { | ||
| return ErrDataSize | ||
| } | ||
| v.Minor = uint16(c) | ||
|
|
||
| i += j | ||
| c, j = binary.Uvarint(data[i:]) | ||
| if j <= 0 { | ||
| return ErrDataSize | ||
| } | ||
| v.Patch = uint16(c) | ||
|
|
||
| i += j | ||
| c, j = binary.Uvarint(data[i:]) | ||
| if j <= 0 { | ||
| return ErrDataSize | ||
| } | ||
|
|
||
| i += j | ||
| if c > 0 { | ||
| if int(c) > len(data[i:]) { | ||
| return ErrDataSize | ||
| } | ||
| v.PreRelease = string(data[i : i+int(c)]) | ||
| } else { | ||
| v.PreRelease = "" | ||
| } | ||
|
|
||
| i += int(c) | ||
| c, j = binary.Uvarint(data[i:]) | ||
| if j <= 0 { | ||
| return ErrDataSize | ||
| } | ||
|
|
||
| i += j | ||
| if c > 0 { | ||
| if int(c) > len(data[i:]) { | ||
| return ErrDataSize | ||
| } | ||
| v.BuildMeta = string(data[i : i+int(c)]) | ||
| } else { | ||
| v.BuildMeta = "" | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UnmarshalBinary panics on malformed input dataMedium Severity
|
||
|
|
||
| func (v Version) MarshalText() ([]byte, error) { | ||
| return []byte(v.String()), nil | ||
| } | ||
|
|
@@ -262,6 +347,25 @@ func (v *Version) UnmarshalJSON(data []byte) error { | |
| return nil | ||
| } | ||
|
|
||
| func (v Version) MarshalYAML() (interface{}, error) { | ||
| return v.String(), nil | ||
| } | ||
|
|
||
| func (v *Version) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
| var vers string | ||
| if err := unmarshal(&vers); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| parsed, err := Parse(vers) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| *v = parsed | ||
| return nil | ||
| } | ||
|
|
||
| //=========================================================================== | ||
| // SQL Interfaces | ||
| //=========================================================================== | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.