Extend type specifiers to cover maps
This commit is contained in:
@@ -53,6 +53,8 @@ def types(args, parentheses=True):
|
||||
for arg in args:
|
||||
if arg.type == Type.LIST:
|
||||
output.append(listTypes(arg.value, []))
|
||||
if arg.type == Type.MAP:
|
||||
output.append(mapTypes(arg.value, {}))
|
||||
else:
|
||||
output.append(arg.type.name.lower())
|
||||
return f"({', '.join(output)})" if parentheses else ', '.join(output)
|
||||
@@ -64,6 +66,22 @@ def listTypes(l, output=None):
|
||||
for item in l:
|
||||
if item.type == Type.LIST:
|
||||
output.append(listTypes(item.value, []))
|
||||
if item.type == Type.MAP:
|
||||
output.append(mapTypes(item.value, {}))
|
||||
else:
|
||||
output.append(item.type.name.lower())
|
||||
return f"{Type.LIST.name.lower()}<{', '.join(set(output))}>"
|
||||
return f"{Type.LIST.name.lower()}<{', '.join(set(output))}>"
|
||||
|
||||
def mapTypes(map, output=None):
|
||||
if output is None:
|
||||
output = {}
|
||||
|
||||
for k, v in map.items():
|
||||
if v.type == Type.LIST:
|
||||
output[k] = (listTypes(v.value, []))
|
||||
elif v.type == Type.MAP:
|
||||
output[k] = mapTypes(v.value, {})
|
||||
else:
|
||||
output[k] = v.type.name.lower()
|
||||
|
||||
return f"{Type.MAP.name.lower()}<{', '.join(set([ k.type.name.lower() for k, v in output.items() ]))}><{', '.join(set([ str(v) for k, v in output.items() ]))}>"
|
||||
@@ -120,6 +120,18 @@ def listOfMatchers(*matchers):
|
||||
|
||||
return Matcher(Type.LIST, check, f"{Type.LIST.name.lower()}<{', '.join([m.string for m in matchers])}>")
|
||||
|
||||
def mapOfMatchers(keyMatchers, valueMatchers):
|
||||
def check(map):
|
||||
matched = 0
|
||||
for key, value in map.value.items():
|
||||
matched += 1 if any(matcher.match(key) for matcher in keyMatchers) \
|
||||
and any(matcher.match(value) for matcher in valueMatchers) else 0
|
||||
|
||||
return matched == len(map.value)
|
||||
|
||||
return Matcher(Type.MAP, check, f"{Type.MAP.name.lower()}<{', '.join([m.string for m in keyMatchers])}><{', '.join([m.string for m in valueMatchers])}>")
|
||||
|
||||
|
||||
def listMatches(*pattern):
|
||||
def check(value):
|
||||
return signature(*pattern).check(value.value)[0]
|
||||
|
||||
Reference in New Issue
Block a user