Cmdr icon indicating copy to clipboard operation
Cmdr copied to clipboard

Variant type

Open evaera opened this issue 5 years ago • 2 comments

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 avatar May 07 '19 07:05 evaera

There will be no autocomplete

No custom autocomplete?

LoganDark avatar May 07 '19 07:05 LoganDark

@LoganDark That is covered in #50

evaera avatar May 07 '19 07:05 evaera