Created
July 15, 2015 16:48
-
-
Save arschles/c3c0a73277a7fc191b2f to your computer and use it in GitHub Desktop.
command parser for https://github.com/arschles/go-progprobs/blob/master/aof.md
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
var ( | |
ErrInvalidCommand = errors.New("invalid command") | |
ErrInvalidNumArgs = errors.New("wrong num args") | |
ErrInvalidInt = errors.New("invalid int") | |
) | |
const ( | |
create = "CREATE" | |
modify = "MODIFY" | |
set = "SET" | |
del = "DELETE" | |
) | |
func NewCommand(slice []string) (*Command, error) { | |
if len(slice) < 2 { | |
return nil, ErrInvalidCommand | |
} | |
cmd := &Command{} | |
switch slice[0] { | |
case create: | |
cmd.Key = slice[1] | |
i, err := strconv.Atoi(slice[2]) | |
if err != nil { | |
return nil, ErrInvalidInt | |
} | |
cmd.Val = i | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment