Files
tasks/components/nexusoperations/link_converter.go
Rodrigo ZhouandDavid Reiss bf737c418c Update nexus link converter (#6460)
## What changed?
<!-- Describe what has changed in this PR -->
Update nexus link converter

## Why?
<!-- Tell your future self why have you made these changes -->
Sync with code in SDK.

## 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) -->
2024-10-04 14:53:50 -07:00

172 lines
6.0 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 temporalio/temporal/components/nexusoperations/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 (
"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"
urlTemplate = "temporal://" + urlPathTemplate
linkWorkflowEventReferenceTypeKey = "referenceType"
linkEventReferenceEventIDKey = "eventID"
linkEventReferenceEventTypeKey = "eventType"
)
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,
))
)
// ConvertLinkWorkflowEventToNexusLink converts a Link_WorkflowEvent type to Nexus Link.
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)
}
return nexus.Link{
URL: u,
Type: string(we.ProtoReflect().Descriptor().FullName()),
}
}
// ConvertNexusLinkToLinkWorkflowEvent converts a Nexus Link to Link_WorkflowEvent.
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, fmt.Errorf("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 string((&commonpb.Link_WorkflowEvent_EventReference{}).ProtoReflect().Descriptor().Name()):
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,
}
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, string(eventRef.ProtoReflect().Descriptor().Name()))
if eventRef.GetEventId() > 0 {
values.Set(linkEventReferenceEventIDKey, strconv.FormatInt(eventRef.GetEventId(), 10))
}
values.Set(linkEventReferenceEventTypeKey, 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(linkEventReferenceEventIDKey)
if eventIDValue != "" {
eventRef.EventId, err = strconv.ParseInt(queryValues.Get(linkEventReferenceEventIDKey), 10, 64)
if err != nil {
return nil, err
}
}
eventRef.EventType, err = enumspb.EventTypeFromString(queryValues.Get(linkEventReferenceEventTypeKey))
if err != nil {
return nil, err
}
return eventRef, nil
}