|
| 1 | +// Copyright (c) 2025 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package messagequeue |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "google.golang.org/protobuf/proto" |
| 21 | + |
| 22 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 23 | +) |
| 24 | + |
| 25 | +type idPayload interface { |
| 26 | + proto.Message |
| 27 | + GetId() string |
| 28 | + GetQueue() string |
| 29 | +} |
| 30 | + |
| 31 | +func idOnlyMessage(key TopicKey, id, queue string) (idPayload, error) { |
| 32 | + switch key { |
| 33 | + case TopicKeyValidate: |
| 34 | + return &Validate{Id: id, Queue: queue}, nil |
| 35 | + case TopicKeyBatch: |
| 36 | + return &Batch{Id: id, Queue: queue}, nil |
| 37 | + case TopicKeyDependencyAnalysis: |
| 38 | + return &DependencyAnalysis{Id: id, Queue: queue}, nil |
| 39 | + case TopicKeySpeculate: |
| 40 | + return &Speculate{Id: id, Queue: queue}, nil |
| 41 | + case TopicKeyBuild: |
| 42 | + return &Build{Id: id, Queue: queue}, nil |
| 43 | + case TopicKeyBuildSignal: |
| 44 | + return &BuildSignal{Id: id, Queue: queue}, nil |
| 45 | + case TopicKeyLand: |
| 46 | + return &Merge{Id: id, Queue: queue}, nil |
| 47 | + case TopicKeyConclude: |
| 48 | + return &Conclude{Id: id, Queue: queue}, nil |
| 49 | + default: |
| 50 | + return nil, fmt.Errorf("topic %q does not carry an id-only payload", key) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// MarshalID serializes the id-only payload bound to key. Start, cancel, and log |
| 55 | +// are not id-only; callers marshal those messages directly. |
| 56 | +func MarshalID(key TopicKey, id, queue string) ([]byte, error) { |
| 57 | + m, err := idOnlyMessage(key, id, queue) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + return Marshal(m) |
| 62 | +} |
| 63 | + |
| 64 | +// UnmarshalID reads id and queue from the id-only payload bound to key. |
| 65 | +// Consumers pass the topic they subscribe to so a field added to that message |
| 66 | +// is decoded rather than discarded as unknown on a different type. |
| 67 | +func UnmarshalID(key TopicKey, b []byte) (id, queue string, err error) { |
| 68 | + m, err := idOnlyMessage(key, "", "") |
| 69 | + if err != nil { |
| 70 | + return "", "", err |
| 71 | + } |
| 72 | + if err := Unmarshal(b, m); err != nil { |
| 73 | + return "", "", err |
| 74 | + } |
| 75 | + return m.GetId(), m.GetQueue(), nil |
| 76 | +} |
| 77 | + |
| 78 | +// UnmarshalLandRequest reads a start payload into the gateway-owned land request. |
| 79 | +func UnmarshalLandRequest(b []byte) (entity.LandRequest, error) { |
| 80 | + m := &Start{} |
| 81 | + if err := Unmarshal(b, m); err != nil { |
| 82 | + return entity.LandRequest{}, err |
| 83 | + } |
| 84 | + return LandRequestFromStart(m), nil |
| 85 | +} |
| 86 | + |
| 87 | +// UnmarshalCancelRequest reads a cancel payload into the domain cancellation. |
| 88 | +func UnmarshalCancelRequest(b []byte) (entity.CancelRequest, error) { |
| 89 | + m := &Cancel{} |
| 90 | + if err := Unmarshal(b, m); err != nil { |
| 91 | + return entity.CancelRequest{}, err |
| 92 | + } |
| 93 | + return CancelToEntity(m), nil |
| 94 | +} |
| 95 | + |
| 96 | +// UnmarshalRequestLog reads a log payload into a request-log entry. |
| 97 | +func UnmarshalRequestLog(b []byte) (entity.RequestLog, error) { |
| 98 | + m := &Log{} |
| 99 | + if err := Unmarshal(b, m); err != nil { |
| 100 | + return entity.RequestLog{}, err |
| 101 | + } |
| 102 | + return LogToEntity(m), nil |
| 103 | +} |
| 104 | + |
| 105 | +// UnmarshalRequestID reads a request-scoped id-only payload (validate or batch). |
| 106 | +func UnmarshalRequestID(key TopicKey, b []byte) (entity.RequestID, error) { |
| 107 | + switch key { |
| 108 | + case TopicKeyValidate, TopicKeyBatch: |
| 109 | + default: |
| 110 | + return entity.RequestID{}, fmt.Errorf("topic %q does not carry a request-id payload", key) |
| 111 | + } |
| 112 | + id, queue, err := UnmarshalID(key, b) |
| 113 | + if err != nil { |
| 114 | + return entity.RequestID{}, err |
| 115 | + } |
| 116 | + return entity.RequestID{ID: id, Queue: queue}, nil |
| 117 | +} |
| 118 | + |
| 119 | +// UnmarshalBatchID reads a batch-scoped id-only payload. |
| 120 | +func UnmarshalBatchID(key TopicKey, b []byte) (entity.BatchID, error) { |
| 121 | + switch key { |
| 122 | + case TopicKeyDependencyAnalysis, TopicKeySpeculate, TopicKeyBuild, TopicKeyLand, TopicKeyConclude: |
| 123 | + default: |
| 124 | + return entity.BatchID{}, fmt.Errorf("topic %q does not carry a batch-id payload", key) |
| 125 | + } |
| 126 | + id, queue, err := UnmarshalID(key, b) |
| 127 | + if err != nil { |
| 128 | + return entity.BatchID{}, err |
| 129 | + } |
| 130 | + return entity.BatchID{ID: id, Queue: queue}, nil |
| 131 | +} |
| 132 | + |
| 133 | +// UnmarshalBuildID reads a buildsignal payload. |
| 134 | +func UnmarshalBuildID(key TopicKey, b []byte) (entity.BuildID, error) { |
| 135 | + if key != TopicKeyBuildSignal { |
| 136 | + return entity.BuildID{}, fmt.Errorf("topic %q does not carry a build-id payload", key) |
| 137 | + } |
| 138 | + id, queue, err := UnmarshalID(key, b) |
| 139 | + if err != nil { |
| 140 | + return entity.BuildID{}, err |
| 141 | + } |
| 142 | + return entity.BuildID{ID: id, Queue: queue}, nil |
| 143 | +} |
0 commit comments