Commit fd9f58fd by Iustin Pop

### Introduce a TMaybe combinator

```

We have many cases in the code where we write TOr(TNone, a), so let's
introduce a combinator that simplifies this case.

Beside replacing the above with TMaybe(a), I did a few other parameter
fixes:

- noop change TOr(TNone, TDict) to TMaybeDict
- noop change TOr(TNone, TNonEmptyString) to TMaybeString
- OpClusterSetParams, change master netmask from any integer to a
non-negative integer

(I can move the last one to a separate patch if desired)
parent 2b5b0fe9
 ... ... @@ -310,19 +310,28 @@ def TRegex(pobj): return desc(TAnd(TString, pobj.match)) def TMaybe(test): """Wrap a test in a TOr(TNone, test). This makes it easier to define TMaybe* types. """ return TOr(TNone, test) # Type aliases #: a non-empty string TNonEmptyString = WithDesc("NonEmptyString")(TAnd(TString, TTrue)) #: a maybe non-empty string TMaybeString = TOr(TNone, TNonEmptyString) TMaybeString = TMaybe(TNonEmptyString) #: a maybe boolean (bool or none) TMaybeBool = TOr(TNone, TBool) TMaybeBool = TMaybe(TBool) #: Maybe a dictionary (dict or None) TMaybeDict = TOr(TNone, TDict) TMaybeDict = TMaybe(TDict) #: a non-negative integer (value >= 0) TNonNegativeInt = \ ... ... @@ -333,7 +342,7 @@ TPositiveInt = \ TAnd(TInt, WithDesc("GreaterThanZero")(lambda v: v > 0)) #: a maybe positive integer (positive integer or None) TMaybePositiveInt = TOr(TNone, TPositiveInt) TMaybePositiveInt = TMaybe(TPositiveInt) #: a negative integer (value < 0) TNegativeInt = \ ... ... @@ -371,7 +380,7 @@ def TListOf(my_type): return desc(TAnd(TList, lambda lst: compat.all(my_type(v) for v in lst))) TMaybeListOf = lambda item_type: TOr(TNone, TListOf(item_type)) TMaybeListOf = lambda item_type: TMaybe(TListOf(item_type)) def TDictOf(key_type, val_type): ... ...
 ... ... @@ -143,7 +143,7 @@ class TestTypeChecks(unittest.TestCase): self.assertFalse(fn(None)) def testOr(self): fn = ht.TOr(ht.TNone, ht.TAnd(ht.TString, ht.TIsLength(5))) fn = ht.TMaybe(ht.TAnd(ht.TString, ht.TIsLength(5))) self.assertTrue(fn("12345")) self.assertTrue(fn(None)) self.assertFalse(fn(1)) ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!