mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-30 14:56:19 +00:00
## What changed? <!-- Describe what has changed in this PR --> Copy nexus link converter from sdk-go ## Why? <!-- Tell your future self why have you made these changes --> ## How did you test it? <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> ## Potential risks <!-- Assuming the worst case, what can be broken when deploying this change to production? --> ## Documentation <!-- Have you made sure this change doesn't falsify anything currently stated in `docs/`? If significant new behavior is added, have you described that in `docs/`? --> ## Is hotfix candidate? <!-- Is this PR a hotfix candidate or does it require a notification to be sent to the broader community? (Yes/No) -->
209 lines
7.4 KiB
Go
209 lines
7.4 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2024 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
// This file is duplicated in sdk-go/temporalnexus/link_converter.go.
|
|
// Any changes here or there must be replicated. This is temporary until the
|
|
// temporal repo updates to the most recent SDK version.
|
|
|
|
package nexusoperations
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"github.com/nexus-rpc/sdk-go/nexus"
|
|
commonpb "go.temporal.io/api/common/v1"
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
)
|
|
|
|
const (
|
|
urlSchemeTemporalKey = "temporal"
|
|
urlPathNamespaceKey = "namespace"
|
|
urlPathWorkflowIDKey = "workflowID"
|
|
urlPathRunIDKey = "runID"
|
|
urlPathTemplate = "/namespaces/%s/workflows/%s/%s/history"
|
|
|
|
linkWorkflowEventReferenceTypeKey = "referenceType"
|
|
linkEventIDKey = "eventID"
|
|
linkEventTypeKey = "eventType"
|
|
linkRequestIDKey = "requestID"
|
|
)
|
|
|
|
var (
|
|
rePatternNamespace = fmt.Sprintf(`(?P<%s>[^/]+)`, urlPathNamespaceKey)
|
|
rePatternWorkflowID = fmt.Sprintf(`(?P<%s>[^/]+)`, urlPathWorkflowIDKey)
|
|
rePatternRunID = fmt.Sprintf(`(?P<%s>[^/]+)`, urlPathRunIDKey)
|
|
urlPathRE = regexp.MustCompile(fmt.Sprintf(
|
|
`^/namespaces/%s/workflows/%s/%s/history$`,
|
|
rePatternNamespace,
|
|
rePatternWorkflowID,
|
|
rePatternRunID,
|
|
))
|
|
eventReferenceType = string((&commonpb.Link_WorkflowEvent_EventReference{}).ProtoReflect().Descriptor().Name())
|
|
requestIDReferenceType = string((&commonpb.Link_WorkflowEvent_RequestIdReference{}).ProtoReflect().Descriptor().Name())
|
|
)
|
|
|
|
// ConvertLinkWorkflowEventToNexusLink converts a Link_WorkflowEvent type to Nexus Link.
|
|
//
|
|
// NOTE: Experimental
|
|
func ConvertLinkWorkflowEventToNexusLink(we *commonpb.Link_WorkflowEvent) nexus.Link {
|
|
u := &url.URL{
|
|
Scheme: urlSchemeTemporalKey,
|
|
Path: fmt.Sprintf(urlPathTemplate, we.GetNamespace(), we.GetWorkflowId(), we.GetRunId()),
|
|
RawPath: fmt.Sprintf(
|
|
urlPathTemplate,
|
|
url.PathEscape(we.GetNamespace()),
|
|
url.PathEscape(we.GetWorkflowId()),
|
|
url.PathEscape(we.GetRunId()),
|
|
),
|
|
}
|
|
|
|
switch ref := we.GetReference().(type) {
|
|
case *commonpb.Link_WorkflowEvent_EventRef:
|
|
u.RawQuery = convertLinkWorkflowEventEventReferenceToURLQuery(ref.EventRef)
|
|
case *commonpb.Link_WorkflowEvent_RequestIdRef:
|
|
u.RawQuery = convertLinkWorkflowEventRequestIdReferenceToURLQuery(ref.RequestIdRef)
|
|
}
|
|
return nexus.Link{
|
|
URL: u,
|
|
Type: string(we.ProtoReflect().Descriptor().FullName()),
|
|
}
|
|
}
|
|
|
|
// ConvertNexusLinkToLinkWorkflowEvent converts a Nexus Link to Link_WorkflowEvent.
|
|
//
|
|
// NOTE: Experimental
|
|
func ConvertNexusLinkToLinkWorkflowEvent(link nexus.Link) (*commonpb.Link_WorkflowEvent, error) {
|
|
we := &commonpb.Link_WorkflowEvent{}
|
|
if link.Type != string(we.ProtoReflect().Descriptor().FullName()) {
|
|
return nil, fmt.Errorf(
|
|
"cannot parse link type %q to %q",
|
|
link.Type,
|
|
we.ProtoReflect().Descriptor().FullName(),
|
|
)
|
|
}
|
|
|
|
if link.URL.Scheme != urlSchemeTemporalKey {
|
|
return nil, fmt.Errorf(
|
|
"failed to parse link to Link_WorkflowEvent: invalid scheme: %s",
|
|
link.URL.Scheme,
|
|
)
|
|
}
|
|
|
|
matches := urlPathRE.FindStringSubmatch(link.URL.EscapedPath())
|
|
if len(matches) != 4 {
|
|
return nil, errors.New("failed to parse link to Link_WorkflowEvent: malformed URL path")
|
|
}
|
|
|
|
var err error
|
|
we.Namespace, err = url.PathUnescape(matches[urlPathRE.SubexpIndex(urlPathNamespaceKey)])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse link to Link_WorkflowEvent: %w", err)
|
|
}
|
|
|
|
we.WorkflowId, err = url.PathUnescape(matches[urlPathRE.SubexpIndex(urlPathWorkflowIDKey)])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse link to Link_WorkflowEvent: %w", err)
|
|
}
|
|
|
|
we.RunId, err = url.PathUnescape(matches[urlPathRE.SubexpIndex(urlPathRunIDKey)])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse link to Link_WorkflowEvent: %w", err)
|
|
}
|
|
|
|
switch refType := link.URL.Query().Get(linkWorkflowEventReferenceTypeKey); refType {
|
|
case eventReferenceType:
|
|
eventRef, err := convertURLQueryToLinkWorkflowEventEventReference(link.URL.Query())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse link to Link_WorkflowEvent: %w", err)
|
|
}
|
|
we.Reference = &commonpb.Link_WorkflowEvent_EventRef{
|
|
EventRef: eventRef,
|
|
}
|
|
case requestIDReferenceType:
|
|
requestIDRef, err := convertURLQueryToLinkWorkflowEventRequestIdReference(link.URL.Query())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse link to Link_WorkflowEvent: %w", err)
|
|
}
|
|
we.Reference = &commonpb.Link_WorkflowEvent_RequestIdRef{
|
|
RequestIdRef: requestIDRef,
|
|
}
|
|
default:
|
|
return nil, fmt.Errorf(
|
|
"failed to parse link to Link_WorkflowEvent: unknown reference type: %q",
|
|
refType,
|
|
)
|
|
}
|
|
|
|
return we, nil
|
|
}
|
|
|
|
func convertLinkWorkflowEventEventReferenceToURLQuery(eventRef *commonpb.Link_WorkflowEvent_EventReference) string {
|
|
values := url.Values{}
|
|
values.Set(linkWorkflowEventReferenceTypeKey, eventReferenceType)
|
|
if eventRef.GetEventId() > 0 {
|
|
values.Set(linkEventIDKey, strconv.FormatInt(eventRef.GetEventId(), 10))
|
|
}
|
|
values.Set(linkEventTypeKey, eventRef.GetEventType().String())
|
|
return values.Encode()
|
|
}
|
|
|
|
func convertURLQueryToLinkWorkflowEventEventReference(queryValues url.Values) (*commonpb.Link_WorkflowEvent_EventReference, error) {
|
|
var err error
|
|
eventRef := &commonpb.Link_WorkflowEvent_EventReference{}
|
|
eventIDValue := queryValues.Get(linkEventIDKey)
|
|
if eventIDValue != "" {
|
|
eventRef.EventId, err = strconv.ParseInt(queryValues.Get(linkEventIDKey), 10, 64)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
eventRef.EventType, err = enumspb.EventTypeFromString(queryValues.Get(linkEventTypeKey))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return eventRef, nil
|
|
}
|
|
|
|
func convertLinkWorkflowEventRequestIdReferenceToURLQuery(requestIDRef *commonpb.Link_WorkflowEvent_RequestIdReference) string {
|
|
values := url.Values{}
|
|
values.Set(linkWorkflowEventReferenceTypeKey, requestIDReferenceType)
|
|
values.Set(linkRequestIDKey, requestIDRef.GetRequestId())
|
|
values.Set(linkEventTypeKey, requestIDRef.GetEventType().String())
|
|
return values.Encode()
|
|
}
|
|
|
|
func convertURLQueryToLinkWorkflowEventRequestIdReference(queryValues url.Values) (*commonpb.Link_WorkflowEvent_RequestIdReference, error) {
|
|
var err error
|
|
requestIDRef := &commonpb.Link_WorkflowEvent_RequestIdReference{
|
|
RequestId: queryValues.Get(linkRequestIDKey),
|
|
}
|
|
requestIDRef.EventType, err = enumspb.EventTypeFromString(queryValues.Get(linkEventTypeKey))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return requestIDRef, nil
|
|
}
|