Esempio n. 1
0
 def set(self, val):
     if val is None:
         raise ValueError('provided value must not be None. To delete a value, please use del.')
     tpe = self.tpe
     typed_value = to_typed_value(tpe, val)
     self.typed_value = typed_value
     self.dirty = True
 def with_invocation(self, arg, tpe, caller=None):
     invocation = self.to_function.invocation.invocations.add()
     if caller:
         (ns, type, id) = caller
         InvocationBuilder.set_address(ns, type, id, invocation.caller)
     invocation.argument.CopyFrom(to_typed_value(tpe, arg))
     return self
Esempio n. 3
0
    def test_message(self):
        typed_value = to_typed_value(statefun.StringType, "hello world")
        msg = statefun.Message(target_typename="foo/bar",
                               target_id="1",
                               typed_value=typed_value)

        self.assertTrue(msg.is_string())
        self.assertEqual(msg.as_string(), "hello world")
Esempio n. 4
0
def egress_message_builder(target_typename: str, value: typing.Any,
                           value_type: Type):
    """
    Create a generic egress record.

    To use Kafka specific egress please use kafka_egress_message(), and for Kinesis please use
    kinesis_egress_message().
    """
    if not target_typename:
        raise ValueError("target typename is missing")
    if value is None:
        raise ValueError("value can not be missing")
    typed_value = to_typed_value(type=value_type, value=value)
    return EgressMessage(typename=target_typename, typed_value=typed_value)
Esempio n. 5
0
def message_builder(target_typename: str, target_id: str, **kwargs) -> Message:
    """
    Build a Message that can be sent to any other function.
    :param target_typename: The TypeName represented as a string of the form <namespace>/<name> of the
           target function.
    :param target_id: The id of the target function
    :param kwargs: This specify the value type to attach to this message. The following arguments are supported:
                    int_value=<an int>,
                    float_value=<a float>
                    long_value=<a signed 64 bit integer>
                    str_value=<str>
                    double_value=<double>
                    bool_value=<bool>
                    ...
                    value=<arbitrary value>, value_type=<a StateFun Type for this value>
    :return: A Message object, that can be sent.
    """
    if len(kwargs) == 2:
        value, value_type = kwargs["value"], kwargs["value_type"]
    elif len(kwargs) == 1:
        # expecting: <type>_value : value
        # for example one of the following:
        #              int_value=1
        #              str_value="hello world"
        #              long_value= 5511
        type_keyword, value = next(iter(kwargs.items()))
        value_type = PRIMITIVE_GETTERS.get(type_keyword)
    else:
        raise TypeError(
            f"Wrong number of value keywords given: {kwargs}, there must be exactly one of:"
            f"\nint_value=.."
            f"\nfloat_value.."
            f"\netc'"
            f"\nor:"
            f"\nvalue=.. ,value_type=.. ")
    if value is None:
        raise ValueError("value can not be missing")
    if not value_type:
        raise ValueError(
            "Could not deduce the value type, please specify the type explicitly. via passing: value=<the value>, "
            "value_type=<the type>")
    typed_value = to_typed_value(type=value_type, value=value)
    return Message(target_typename=target_typename,
                   target_id=target_id,
                   typed_value=typed_value)
 def with_state(self, name, value=None, type=None):
     state = self.to_function.invocation.state.add()
     state.state_name = name
     if value is not None:
         state.state_value.CopyFrom(to_typed_value(type, value))
     return self
Esempio n. 7
0
 def __init__(self, name, val, tpe):
     self.state_name = name
     self.state_value = to_typed_value(tpe, val)