Skip to content

Commit

Permalink
add CommandContext.Int32,Int64,Float64
Browse files Browse the repository at this point in the history
return correct argument type when parsing
  • Loading branch information
robinbraemer committed Jun 16, 2021
1 parent b0546f7 commit efe2827
Showing 1 changed file with 52 additions and 17 deletions.
69 changes: 52 additions & 17 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,36 @@ type ArgumentTypeFuncs struct {
func (t *ArgumentTypeFuncs) Parse(rd *StringReader) (interface{}, error) { return t.ParseFn(rd) }
func (t *ArgumentTypeFuncs) String() string { return t.Name }

// Int returns the parsed int argument from the command context.
// It returns the zero-value if not found.
// Int is the same as CommandContext.Int32.
func (c *CommandContext) Int(argumentName string) int {
return int(c.Int32(argumentName))
}

// Int32 returns the parsed int32 argument from the command context.
// It returns the zero-value if not found.
func (c *CommandContext) Int32(argumentName string) int32 {
if c.Arguments == nil {
return 0
}
r, ok := c.Arguments[argumentName]
if !ok {
return 0
}
v, _ := r.Result.(int32)
return v
}

// Int64 returns the parsed int64 argument from the command context.
// It returns the zero-value if not found.
func (c *CommandContext) Int64(argumentName string) int64 {
if c.Arguments == nil {
return 0
}
r, ok := c.Arguments[argumentName]
if !ok {
return 0
}
v, _ := r.Result.(int)
v, _ := r.Result.(int64)
return v
}

Expand All @@ -101,6 +120,20 @@ func (c *CommandContext) Bool(argumentName string) bool {
return v
}

// Float32 returns the parsed float32 argument from the command context.
// It returns the zero-value if not found.
func (c *CommandContext) Float32(argumentName string) float32 {
if c.Arguments == nil {
return 0
}
r, ok := c.Arguments[argumentName]
if !ok {
return 0
}
v, _ := r.Result.(float32)
return v
}

// Float64 returns the parsed float64 argument from the command context.
// It returns the zero-value if not found.
func (c *CommandContext) Float64(argumentName string) float64 {
Expand Down Expand Up @@ -154,7 +187,7 @@ func (t StringType) Parse(rd *StringReader) (interface{}, error) {
}

type BoolArgumentType struct{}
type Int32ArgumentType struct{ Min, Max int }
type Int32ArgumentType struct{ Min, Max int32 }
type Int64ArgumentType struct{ Min, Max int64 }
type Float32ArgumentType struct{ Min, Max float32 }
type Float64ArgumentType struct{ Min, Max float64 }
Expand Down Expand Up @@ -183,53 +216,55 @@ func (t *BoolArgumentType) Suggestions(_ *CommandContext, builder *SuggestionsBu
}
func (t *Int32ArgumentType) String() string { return "int32" }
func (t *Int32ArgumentType) Parse(rd *StringReader) (interface{}, error) {
return parseInt(rd, 32, int64(t.Min), int64(t.Max))
i, err := parseInt(rd, 32, int64(t.Min), int64(t.Max))
return int32(i), err
}
func (t *Int64ArgumentType) String() string { return "int64" }
func (t *Int64ArgumentType) Parse(rd *StringReader) (interface{}, error) {
return parseInt(rd, 64, t.Min, t.Max)
}
func parseInt(rd *StringReader, bitSize int, min, max int64) (interface{}, error) {
func parseInt(rd *StringReader, bitSize int, min, max int64) (int64, error) {
start := rd.Cursor
result, err := rd.readInt(bitSize)
if err != nil {
return nil, err
return 0, err
}
if result < min {
rd.Cursor = start
return nil, &CommandSyntaxError{Err: fmt.Errorf("%w (%d < %d)",
return 0, &CommandSyntaxError{Err: fmt.Errorf("%w (%d < %d)",
ErrArgumentIntegerTooLow, result, min)}
}
if result > max {
rd.Cursor = start
return nil, &CommandSyntaxError{Err: fmt.Errorf("%w (%d > %d)",
return 0, &CommandSyntaxError{Err: fmt.Errorf("%w (%d > %d)",
ErrArgumentIntegerTooHigh, result, max)}
}
return result, nil
}

func (t *Float32ArgumentType) String() string { return "float32" }
func (t *Float32ArgumentType) Parse(rd *StringReader) (interface{}, error) {
f, err := parseFloat(rd, 32, float64(t.Min), float64(t.Max))
return float32(f), err
}
func (t *Float64ArgumentType) String() string { return "float64" }
func (t *Float64ArgumentType) Parse(rd *StringReader) (interface{}, error) {
return parseFloat(rd, 64, t.Min, t.Max)
}
func (t *Float32ArgumentType) String() string { return "float32" }
func (t *Float32ArgumentType) Parse(rd *StringReader) (interface{}, error) {
return parseFloat(rd, 32, float64(t.Min), float64(t.Max))
}
func parseFloat(rd *StringReader, bitSize int, min, max float64) (interface{}, error) {
func parseFloat(rd *StringReader, bitSize int, min, max float64) (float64, error) {
start := rd.Cursor
result, err := rd.readFloat(bitSize)
if err != nil {
return nil, err
return 0, err
}
if result < min {
rd.Cursor = start
return nil, &CommandSyntaxError{Err: fmt.Errorf("%w (%f < %f)",
return 0, &CommandSyntaxError{Err: fmt.Errorf("%w (%f < %f)",
ErrArgumentFloatTooLow, result, min)}
}
if result > max {
rd.Cursor = start
return nil, &CommandSyntaxError{Err: fmt.Errorf("%w (%f > %f)",
return 0, &CommandSyntaxError{Err: fmt.Errorf("%w (%f > %f)",
ErrArgumentFloatTooHigh, result, max)}
}
return result, nil
Expand Down

0 comments on commit efe2827

Please sign in to comment.