]> git.giacomolli.uk - onehouse/blobdiff - protocol/message.go
Keep topic and envelope construction private
[onehouse] / protocol / message.go
index 94ea7b582ee25fc252fe9dad504af018c4d51bc9..0fd4ec9195796a53d529112ed41d1d457b469471 100644 (file)
@@ -35,7 +35,7 @@ type Publication struct {
 // EventPayload, BatchPayload, ResultPayload, or HubStatusPayload. Address.Kind
 // distinguishes command, reported, and telemetry value payloads.
 func Parse(topic string, data []byte, retained bool) (Message[any], error) {
-       address, err := ParseTopic(topic)
+       address, err := parseTopic(topic)
        if err != nil {
                return Message[any]{}, fmt.Errorf("parse address: %w", err)
        }
@@ -78,12 +78,12 @@ func eraseMessage[T any](message Message[T], err error) (Message[any], error) {
 }
 
 func buildMessage[T any](address Address, metadata Metadata, payload T) (Publication, error) {
-       topic, err := address.Topic()
+       topic, err := address.topic()
        if err != nil {
                return Publication{}, fmt.Errorf("build address: %w", err)
        }
 
-       data, err := EncodeEnvelope(metadata, payload)
+       data, err := encodeEnvelope(metadata, payload)
        if err != nil {
                return Publication{}, fmt.Errorf("build envelope: %w", err)
        }
@@ -114,7 +114,7 @@ func buildMessage[T any](address Address, metadata Metadata, payload T) (Publica
                        Kind:   Result,
                        ID:     address.ID,
                        Target: metadata.MessageID,
-               }).Topic()
+               }).topic()
                if err != nil {
                        return Publication{}, fmt.Errorf("build command response topic: %w", err)
                }
@@ -137,7 +137,7 @@ func buildMessage[T any](address Address, metadata Metadata, payload T) (Publica
 }
 
 func parseMessage[T any](kind Kind, topic string, data []byte, retained bool) (Message[T], error) {
-       address, err := ParseTopic(topic)
+       address, err := parseTopic(topic)
        if err != nil {
                return Message[T]{}, fmt.Errorf("parse address: %w", err)
        }
@@ -150,7 +150,7 @@ func parseMessage[T any](kind Kind, topic string, data []byte, retained bool) (M
                return Message[T]{}, fmt.Errorf("message must not be retained")
        }
 
-       envelope, err := ParseEnvelope(data)
+       envelope, err := parseEnvelope(data)
        if err != nil {
                return Message[T]{}, fmt.Errorf("parse envelope: %w", err)
        }
@@ -175,7 +175,7 @@ func parseMessage[T any](kind Kind, topic string, data []byte, retained bool) (M
        }, nil
 }
 
-func validatePayload(address Address, envelope Envelope, payload any) error {
+func validatePayload(address Address, envelope wireEnvelope, payload any) error {
        switch value := payload.(type) {
        case DescriptorPayload:
                err := validateDescriptor(value, envelope.Payload)
@@ -268,7 +268,7 @@ func validateDescriptor(payload DescriptorPayload, raw json.RawMessage) error {
        return nil
 }
 
-func validateBatch(payload BatchPayload, envelope Envelope) error {
+func validateBatch(payload BatchPayload, envelope wireEnvelope) error {
        if len(payload.Commands) < 2 || len(payload.Commands) > MaximumCommandBatchSize {
                return fmt.Errorf("batch size must be 2 through %d", MaximumCommandBatchSize)
        }
@@ -285,7 +285,7 @@ func validateBatch(payload BatchPayload, envelope Envelope) error {
                        return fmt.Errorf("batch targets and IDs must be valid and distinct")
                }
 
-               _, err := EncodeEnvelope(command.Metadata, command.Payload)
+               _, err := encodeEnvelope(command.Metadata, command.Payload)
                if err != nil {
                        return fmt.Errorf("validate batch command envelope: %w", err)
                }
@@ -301,7 +301,7 @@ func validateBatch(payload BatchPayload, envelope Envelope) error {
        return nil
 }
 
-func validateHub(payload HubStatusPayload, address Address, envelope Envelope) error {
+func validateHub(payload HubStatusPayload, address Address, envelope wireEnvelope) error {
        if payload.ID != address.ID || envelope.ExpiresAt == nil {
                return fmt.Errorf("hub status requires a matching ID and an expiry")
        }