func()

in statefun-sdk-go/v3/pkg/statefun/message.go [32:80]


func (m MessageBuilder) ToMessage() (Message, error) {
	if m.Target == (Address{}) {
		return Message{}, errors.New("a message must have a non-empty target")
	}

	if m.Value == nil {
		return Message{}, errors.New("a message cannot have a nil value")
	}

	if m.ValueType == nil {
		switch m.Value.(type) {
		case int:
			return Message{}, errors.New("ambiguous integer type; please specify int32 or int64")
		case bool, *bool:
			m.ValueType = BoolType
		case int32, *int32:
			m.ValueType = Int32Type
		case int64, *int64:
			m.ValueType = Int64Type
		case float32, *float32:
			m.ValueType = Float32Type
		case float64, *float64:
			m.ValueType = Float64Type
		case string, *string:
			m.ValueType = StringType
		default:
			return Message{}, errors.New("message contains non-primitive type, please supply a non-nil SimpleType")
		}
	}

	buffer := bytes.Buffer{}
	err := m.ValueType.Serialize(&buffer, m.Value)
	if err != nil {
		return Message{}, err
	}

	return Message{
		target: &protocol.Address{
			Namespace: m.Target.FunctionType.GetNamespace(),
			Type:      m.Target.FunctionType.GetType(),
			Id:        m.Target.Id,
		},
		typedValue: &protocol.TypedValue{
			Typename: m.ValueType.GetTypeName().String(),
			HasValue: true,
			Value:    buffer.Bytes(),
		},
	}, nil
}