|
| 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 holds Runway's external message-queue contract: the wire |
| 16 | +// payloads for the merge queues Runway owns, defined by the proto files in |
| 17 | +// proto/ and generated into protopb/. The proto is the language-neutral |
| 18 | +// authority; the generated Go types in protopb are the binding for Go callers. |
| 19 | +// |
| 20 | +// The message types are generated into protopb; this package adds only generic |
| 21 | +// protojson glue (Marshal/Unmarshal) and the topic-key reflection lookup |
| 22 | +// (TopicKeys), so there is no per-message serialization code. Payloads are |
| 23 | +// serialized as protobuf JSON, not binary, so the MySQL-backed queue keeps |
| 24 | +// storing self-describing JSON. The topic key that carries each payload is |
| 25 | +// declared on the message itself via the topic_keys proto option (see |
| 26 | +// api/base/messagequeue). |
| 27 | +// |
| 28 | +// One contract serves two queue pairs because a merge-conflict check is a dry |
| 29 | +// run of a merge: Runway applies the same ordered steps onto the same target |
| 30 | +// branch, and the only difference is whether it commits the result and reports |
| 31 | +// the revisions it produced. The topic key a request arrives on encodes that choice |
| 32 | +// — the merge-conflict-check pair for a dry run, the merge pair for a |
| 33 | +// committing merge — so MergeRequest and MergeResult are identical on both. |
| 34 | +package messagequeue |
| 35 | + |
| 36 | +import ( |
| 37 | + "google.golang.org/protobuf/encoding/protojson" |
| 38 | + "google.golang.org/protobuf/proto" |
| 39 | + |
| 40 | + basemqpb "github.com/uber/submitqueue/api/base/messagequeue/protopb" |
| 41 | + "github.com/uber/submitqueue/api/runway/messagequeue/protopb" |
| 42 | +) |
| 43 | + |
| 44 | +// Wire payload types. These alias the generated protobuf bindings so callers |
| 45 | +// reference the contract through this curated package rather than protopb. |
| 46 | +type ( |
| 47 | + // MergeRequest is the payload a client publishes to one of Runway's merge |
| 48 | + // queues: the merge-conflict-check topic for a dry-run check, the merge |
| 49 | + // topic for a committing merge. |
| 50 | + MergeRequest = protopb.MergeRequest |
| 51 | + // MergeStep is one step of an ordered merge: a single set of change(s) |
| 52 | + // applied with a strategy. |
| 53 | + MergeStep = protopb.MergeStep |
| 54 | + // MergeResult is the payload Runway publishes to the corresponding signal |
| 55 | + // queue once a request completes. |
| 56 | + MergeResult = protopb.MergeResult |
| 57 | + // StepResult reports what happened to a single MergeStep. |
| 58 | + StepResult = protopb.StepResult |
| 59 | + // StepOutput is a single revision a merge step produced on the target branch. |
| 60 | + StepOutput = protopb.StepOutput |
| 61 | +) |
| 62 | + |
| 63 | +// marshalOpts keeps the JSON field names identical to the proto field names |
| 64 | +// (snake_case), so the wire shape matches the declared contract rather than |
| 65 | +// protojson's default lowerCamelCase. Zero-valued fields are omitted. |
| 66 | +var marshalOpts = protojson.MarshalOptions{UseProtoNames: true} |
| 67 | + |
| 68 | +// unmarshalOpts tolerates unknown fields so an additive contract change (a new |
| 69 | +// field a producer sends but this consumer does not yet know) is ignored rather |
| 70 | +// than rejected. |
| 71 | +var unmarshalOpts = protojson.UnmarshalOptions{DiscardUnknown: true} |
| 72 | + |
| 73 | +// Marshal serializes any contract message to protojson bytes for the queue |
| 74 | +// payload, keeping the proto field names (snake_case) on the wire. |
| 75 | +func Marshal(m proto.Message) ([]byte, error) { |
| 76 | + return marshalOpts.Marshal(m) |
| 77 | +} |
| 78 | + |
| 79 | +// Unmarshal deserializes protojson bytes into the contract message m, tolerating |
| 80 | +// unknown fields so an additive contract change is ignored rather than rejected. |
| 81 | +func Unmarshal[T proto.Message](b []byte, m T) error { |
| 82 | + return unmarshalOpts.Unmarshal(b, m) |
| 83 | +} |
| 84 | + |
| 85 | +// TopicKeys returns the stable logical topic keys bound to a message via the |
| 86 | +// topic_keys proto option — not concrete wire names; a caller maps each key to |
| 87 | +// its backend's topic name. Returns nil for a message that declares no keys. |
| 88 | +func TopicKeys(m proto.Message) []string { |
| 89 | + opts := m.ProtoReflect().Descriptor().Options() |
| 90 | + if opts == nil { |
| 91 | + return nil |
| 92 | + } |
| 93 | + keys, ok := proto.GetExtension(opts, basemqpb.E_TopicKeys).([]string) |
| 94 | + if !ok { |
| 95 | + return nil |
| 96 | + } |
| 97 | + return keys |
| 98 | +} |
0 commit comments