Repository navigation
Miscellaneous updates and fixes #45
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
5dba805
57204e1
4256e94
b31f0ce
0d3e845
78c517e
a573deb
119d330
f215731
9ac86e7
c4e4c14
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 |
|---|---|---|
| @@ -1,23 +1,16 @@ | ||
| # Go Linters - GitHub Actions | ||
| name: linters | ||
| on: [push] | ||
| jobs: | ||
|
|
||
| # Check linters on latest-ubuntu with default version of Go. | ||
| on: [push, pull_request] | ||
| jobs: | ||
| lint: | ||
| name: Lint | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/setup-go@v3 | ||
| - uses: actions/setup-go@v5 | ||
| with: | ||
| go-version: "1.23" | ||
| go-version: "1.26" | ||
| - name: Checkout code | ||
| uses: actions/checkout@v2 | ||
| - name: Install golangci-lint | ||
| run: | | ||
| go version | ||
| curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.1.6 | ||
| - name: Run required linters in .golangci.yml plus hard-coded ones here | ||
| run: $(go env GOPATH)/bin/golangci-lint run --timeout=3m | ||
| - name: Run optional linters (not required to pass) | ||
| run: $(go env GOPATH)/bin/golangci-lint run --timeout=3m --issues-exit-code=0 -E dupl -E gocritic -E lll -E prealloc | ||
| run: make -w lint |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,49 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "math" | ||
| "reflect" | ||
| "strconv" | ||
| ) | ||
|
|
||
| // IntOrStringMapFromAny creates a map[IntOrString]any from the provided value | ||
| // if possible, returning an error otherwose. | ||
| func IntOrStringMapFromAny(value any) (map[IntOrString]any, error) { | ||
| valueInfo := reflect.ValueOf(value) | ||
| if valueInfo.Kind() == reflect.Pointer { | ||
| if valueInfo.IsNil() { | ||
| return nil, errors.New("nil pointer") | ||
| } | ||
|
|
||
| value = valueInfo.Elem().Interface() | ||
| } | ||
|
|
||
| var ret map[IntOrString]any | ||
|
|
||
| switch t := value.(type) { | ||
| case map[IntOrString]any: | ||
| ret = t | ||
| case map[string]any: | ||
| ret = make(map[IntOrString]any) | ||
| for k, v := range t { | ||
| ret[IntOrStringFromString(k)] = v | ||
| } | ||
| case map[any]any: | ||
| ret = make(map[IntOrString]any) | ||
| for k, v := range t { | ||
| convertedK, err := IntOrStringFromAny(k) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| ret[convertedK] = v | ||
| } | ||
| default: | ||
| return nil, fmt.Errorf("cannot convert %T to map[eat.IntOrString]any", value) | ||
| } | ||
|
|
||
| return ret, nil | ||
| } | ||
|
|
||
| // IntOrString has an underlying value that is either an int or string. | ||
| type IntOrString struct { | ||
| val any | ||
|
|
@@ -62,6 +102,13 @@ func IntOrStringFromAny(val any) (IntOrString, error) { | |
| return IntOrString{}, fmt.Errorf("out of range: %d", t) | ||
| } | ||
| ret.val = int(t) | ||
| case int: | ||
| ret.val = t | ||
| case uint: | ||
| if uint64(t) > math.MaxInt64 { | ||
| return IntOrString{}, fmt.Errorf("out of range: %d", t) | ||
| } | ||
| ret.val = int(t) | ||
| case float64: | ||
| ret.val = int(t) | ||
| case string: | ||
|
|
@@ -72,7 +119,7 @@ func IntOrStringFromAny(val any) (IntOrString, error) { | |
| ret.val = t | ||
| } | ||
| default: | ||
| return IntOrString{}, fmt.Errorf("unexpected algorithm value: %v(%T)", t, t) | ||
| return IntOrString{}, fmt.Errorf("cannot convert %v (%T) to IntOrString", t, t) | ||
| } | ||
|
|
||
| return ret, nil | ||
|
|
@@ -171,3 +218,12 @@ func (o *IntOrString) UnmarshalJSON(data []byte) error { | |
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (o IntOrString) MarshalText() ([]byte, error) { | ||
| return []byte(o.String()), nil | ||
| } | ||
|
|
||
| func (o *IntOrString) UnmarshalText(data []byte) error { | ||
| *o = IntOrStringFromString(string(data)) | ||
| return nil | ||
| } | ||
|
Comment on lines
+222
to
+229
Contributor
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. these two do not roundrtip: string("1") becomes int(1). Worth documenting it (also, these are public methods and should have their doc strings).
Contributor
Author
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. They do. Regarding doc comments -- I thought that for interface implementations, the comments are on the interface, not the implementation? (e.g. we don't usually put doc comments on
Contributor
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.
oh, right! Sorry for the noise.
It's a method exported by the package, so strictly speaking we should document it. Why not making it private then?
Contributor
Author
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. It needs to be public for the same reason as |
||
Uh oh!
There was an error while loading. Please reload this page.