github.com/TBD54566975/ftl@v0.219.0/internal/model/hostport_mixin.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/alecthomas/types/optional"
     8  )
     9  
    10  // HostPortMixin is a mixin for keys that have an (optional) hostname and a port.
    11  type HostPortMixin struct {
    12  	Hostname optional.Option[string]
    13  	Port     string
    14  }
    15  
    16  func (h *HostPortMixin) String() string {
    17  	if hostname, ok := h.Hostname.Get(); ok {
    18  		return hostname + "-" + h.Port
    19  	}
    20  	return h.Port
    21  }
    22  
    23  func (h *HostPortMixin) Parse(parts []string) error {
    24  	if len(parts) == 0 {
    25  		return fmt.Errorf("expected <hostname>-<port> but got %q", strings.Join(parts, "-"))
    26  	}
    27  	h.Hostname = optional.Zero(strings.Join(parts[:len(parts)-1], "-"))
    28  	h.Port = parts[len(parts)-1]
    29  	return nil
    30  }
    31  
    32  func (h *HostPortMixin) RandomBytes() int { return 10 }