Skip to content
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

Variant type #79

Open
evaera opened this issue May 7, 2019 · 2 comments
Open

Variant type #79

evaera opened this issue May 7, 2019 · 2 comments
Labels
help wanted Contributions welcome scope:built-in type type:enhancement New feature or request

Comments

@evaera
Copy link
Owner

evaera commented May 7, 2019

The variant type represents a value whose type is not known until further processing is done.

There will be no autocomplete (unless overridden as described in #50), and the user can type any string for an argument of this type.

The command implementation will receive an instance of a new Variant class in the slot of the argument.

The Variant class will have methods to further process the data with one of the built-in Cmdr data types.

RawValue: string, the input text.
Parse(type: string | string[]): string | false, string, parses a value
Match(arms: MatchArm[]): any Performs pattern matching

Parse

local parsedType, value = variant:Parse("number")

parsedType is "number" if the parsing was successful, and value is of the parsed type.
parsedType is alternatively false, in which case value will be a string containing the error.

Parse could also receive an array of type names, in which case it attempts to parse down the line until it finds one that is not invalid. For example:

local parsedType, value = variant:Parse({"number", "boolean"})

In this case, the input 1 will return "number", 1, and the input text will return false, "Not a valid boolean or whatever the error message is"

Match

Match accepts an array of matches. Each potential match is an array with two values. The first value is a Lua string pattern. The second is a function, which will receive all captures from the pattern. String patterns which do not match are skipped. Matches are tested in order. The function should return the final value that will be the result from the Match call.

local value = variant:Match({
  {"(.+)%((.+)%)", function(outer, inner)
    SomeCustomType.new(outer, inner)
  end},

  {"%d+", function (text)
    return text, "number"
  end}
})

Optionally return a type name as the second return value to have Cmdr run the first return value through that type's parser before continuing. If this parsing fails, then that pattern match is considered failed and Match will continue to move down the array.

Match returns nil if no matches are found.

@evaera evaera added the type:enhancement New feature or request label May 7, 2019
@LoganDark
Copy link
Contributor

There will be no autocomplete

No custom autocomplete?

@evaera
Copy link
Owner Author

evaera commented May 7, 2019

@LoganDark That is covered in #50

@evaera evaera added the help wanted Contributions welcome label Jul 30, 2019
@evaera evaera mentioned this issue Jun 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Contributions welcome scope:built-in type type:enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants