-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup(all) Merge lint from main (#3358)
Co-authored-by: Asif Nawaz <[email protected]>
- Loading branch information
1 parent
c9ca2f6
commit b1b2a2a
Showing
8 changed files
with
94 additions
and
7 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
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
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
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,47 @@ | ||
package rtypecontrol | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
// PaveArgs converts each arg to its desired type, or returns an error if conversion fails or if the number of arguments is wrong. | ||
// argTypes is a string where each rune specifies the desired type of the arg in the same position: | ||
// 'i': uinet16 (will convert strings, truncate floats, etc) | ||
// 's': Valid only if string. | ||
func PaveArgs(args []any, argTypes string) error { | ||
if len(args) != len(argTypes) { | ||
return fmt.Errorf("wrong number of arguments. Expected %v, got %v", len(argTypes), len(args)) | ||
} | ||
|
||
for i, at := range argTypes { | ||
arg := args[i] | ||
switch at { | ||
case 'i': // uint16 | ||
if s, ok := arg.(string); ok { // Is this a string-encoded int? | ||
ni, err := strconv.Atoi(s) | ||
if err != nil { | ||
return fmt.Errorf("value %q is not a number (uint16 wanted)", arg) | ||
} | ||
args[i] = uint16(ni) | ||
} else if _, ok := arg.(float64); ok { | ||
args[i] = uint16(arg.(float64)) | ||
} else if _, ok := arg.(uint16); ok { | ||
args[i] = arg.(uint16) | ||
} else if _, ok := arg.(int); ok { | ||
args[i] = uint16(arg.(int)) | ||
} else { | ||
return fmt.Errorf("value %q is type %T, expected uint16", arg, arg) | ||
} | ||
|
||
case 's': | ||
if _, ok := arg.(string); ok { | ||
args[i] = arg.(string) | ||
} else { | ||
args[i] = fmt.Sprintf("%v", arg) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
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
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
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
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