def _SetField(self, args, type_info, value): """Sets fields on the arg rdfvalue object.""" if hasattr(type_info, "enum"): try: coerced_obj = type_info.enum[value.upper()] except KeyError: # A bool is an enum but serializes to "1" / "0" which are both not in # enum or reverse_enum. coerced_obj = serialization.FromHumanReadable(type_info.type, value) else: coerced_obj = serialization.FromHumanReadable(type_info.type, value) args.Set(type_info.name, coerced_obj)
def FlatDictToRDFValue(dct: Dict[str, str], cls: Type[_V]) -> _V: """Converts a flat dictionary to an RDF value instance. In the flat dictionary, dots are used to denote a path of nested attributes in the resulting message. Consider the following dictionary: { "foo.bar": "42", "foo.baz.quux": "thud" } It can correspond (depending on the schema) to the following Protocol Buffers message (or some RDF value): foo { bar: 42 baz: { quux: "thud" } } Args: dct: A flat dictionary to convert. cls: An RDF value type to interpret the dictionary as. Returns: An instance of the specified RDF value type. Raises: AttributeError: If the message type does not support dictionary keys. """ result = cls() for key, value in dct.items(): path = key.split(".") # First, we need to lookup the type that the field ought to have: for this # we simply go down the type descriptors. try: attr_cls_getter = lambda cls, name: cls.type_infos[name].type attr_cls = functools.reduce(attr_cls_getter, path, cls) except KeyError: raise AttributeError( f"'{cls}' does not support nested attribute '{key}'") # We do from-string conversion only for non-enum values. Enums will do an # automatic conversion upon assignment. if not issubclass(attr_cls, rdf_structs.EnumNamedValue): value = serialization.FromHumanReadable(attr_cls, value) # Then, we can go down the path to the second-to-last attribute and finally # set the value on the last one. child_attr = functools.reduce(rdf_structs.RDFStruct.Get, path[:-1], result) child_attr.Set(path[-1], value) return result
def FromString(self, string): return serialization.FromHumanReadable(self.rdfclass, string)
def testHumanReadable(self): self.assertIs(serialization.FromHumanReadable(bool, str(True)), True) self.assertIs(serialization.FromHumanReadable(bool, str(False)), False)
def testFromHumanReadableRaisesOnWeirdInput(self): with self.assertRaises(ValueError): serialization.FromHumanReadable(bool, u"yes")
def testFromHumanReadableRaisesOnIncorrectInteger(self): with self.assertRaises(ValueError): serialization.FromHumanReadable(bool, u"2")
def testFromHumanReadableFalse(self): self.assertIs(serialization.FromHumanReadable(bool, u"false"), False) self.assertIs(serialization.FromHumanReadable(bool, u"False"), False) self.assertIs(serialization.FromHumanReadable(bool, u"FALSE"), False) self.assertIs(serialization.FromHumanReadable(bool, u"0"), False)
def testFromHumanReadableTrue(self): self.assertIs(serialization.FromHumanReadable(bool, u"true"), True) self.assertIs(serialization.FromHumanReadable(bool, u"True"), True) self.assertIs(serialization.FromHumanReadable(bool, u"TRUE"), True) self.assertIs(serialization.FromHumanReadable(bool, u"1"), True)