Skip to content

Commit

Permalink
chore: upgrade alecthomas/types (#842)
Browse files Browse the repository at this point in the history
The types were moved into separate packages, so this upgrade couldn't be
automated by Renovate.

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
alecthomas and github-actions[bot] authored Jan 29, 2024
1 parent 8f126ed commit 4769934
Show file tree
Hide file tree
Showing 26 changed files with 213 additions and 212 deletions.
4 changes: 2 additions & 2 deletions backend/common/model/deployment_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"fmt"
"strings"

"github.com/alecthomas/types"
"github.com/alecthomas/types/optional"
)

type DeploymentName string

type MaybeDeploymentName types.Option[DeploymentName]
type MaybeDeploymentName optional.Option[DeploymentName]

var _ interface {
sql.Scanner
Expand Down
4 changes: 2 additions & 2 deletions backend/common/model/request_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"regexp"
"strings"

"github.com/alecthomas/types"
"github.com/alecthomas/types/optional"
)

// A RequestName represents an inbound request into the cluster.
type RequestName string

type MaybeRequestName types.Option[RequestName]
type MaybeRequestName optional.Option[RequestName]

var _ interface {
sql.Scanner
Expand Down
10 changes: 5 additions & 5 deletions backend/common/rpc/headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/http"

"github.com/alecthomas/types"
"github.com/alecthomas/types/optional"

"github.com/TBD54566975/ftl/backend/common/model"
"github.com/TBD54566975/ftl/backend/schema"
Expand Down Expand Up @@ -69,16 +69,16 @@ func GetCallers(header http.Header) ([]*schema.VerbRef, error) {
// GetCaller returns the module.verb of the caller, if any.
//
// Will return an error if the header is malformed.
func GetCaller(header http.Header) (types.Option[*schema.VerbRef], error) {
func GetCaller(header http.Header) (optional.Option[*schema.VerbRef], error) {
headers := header.Values(VerbHeader)
if len(headers) == 0 {
return types.None[*schema.VerbRef](), nil
return optional.None[*schema.VerbRef](), nil
}
ref, err := schema.ParseVerbRef(headers[len(headers)-1])
if err != nil {
return types.None[*schema.VerbRef](), err
return optional.None[*schema.VerbRef](), err
}
return types.Some(ref), nil
return optional.Some(ref), nil
}

// AddCaller to an outgoing request.
Expand Down
10 changes: 5 additions & 5 deletions backend/controller/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"connectrpc.com/connect"
"github.com/alecthomas/types"
"github.com/alecthomas/types/optional"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -269,13 +269,13 @@ func eventsQueryProtoToDAL(pb *pbconsole.EventsQuery) ([]dal.EventFilter, error)
}
query = append(query, dal.FilterIDRange(lowerThan, higherThan))
case *pbconsole.EventsQuery_Filter_Call:
var sourceModule types.Option[string]
var sourceModule optional.Option[string]
if filter.Call.SourceModule != nil {
sourceModule = types.Some(*filter.Call.SourceModule)
sourceModule = optional.Some(*filter.Call.SourceModule)
}
var destVerb types.Option[string]
var destVerb optional.Option[string]
if filter.Call.DestVerb != nil {
destVerb = types.Some(*filter.Call.DestVerb)
destVerb = optional.Some(*filter.Call.DestVerb)
}
query = append(query, dal.FilterCall(sourceModule, filter.Call.DestModule, destVerb))

Expand Down
16 changes: 8 additions & 8 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"connectrpc.com/connect"
"github.com/alecthomas/concurrency"
"github.com/alecthomas/kong"
"github.com/alecthomas/types"
"github.com/alecthomas/types/optional"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jellydator/ttlcache/v3"
Expand Down Expand Up @@ -396,13 +396,13 @@ func (s *Service) StreamDeploymentLogs(ctx context.Context, stream *connect.Clie
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("%s: %w", "invalid deployment key", err))
}
var requestName types.Option[model.RequestName]
var requestName optional.Option[model.RequestName]
if msg.RequestName != nil {
_, rkey, err := model.ParseRequestName(*msg.RequestName)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("%s: %w", "invalid request key", err))
}
requestName = types.Some(rkey)
requestName = optional.Some(rkey)
}

err = s.dal.InsertLogEvent(ctx, &dal.LogEvent{
Expand All @@ -412,7 +412,7 @@ func (s *Service) StreamDeploymentLogs(ctx context.Context, stream *connect.Clie
Level: msg.LogLevel,
Attributes: msg.Attributes,
Message: msg.Message,
Error: types.Ptr(msg.Error),
Error: optional.Ptr(msg.Error),
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -671,17 +671,17 @@ func (s *Service) Call(ctx context.Context, req *connect.Request[ftlv1.CallReque
headers.AddCaller(req.Header(), schema.VerbRefFromProto(req.Msg.Verb))

resp, err := client.verb.Call(ctx, req)
var maybeResponse types.Option[*ftlv1.CallResponse]
var maybeResponse optional.Option[*ftlv1.CallResponse]
if resp != nil {
maybeResponse = types.Some(resp.Msg)
maybeResponse = optional.Some(resp.Msg)
}
s.recordCall(ctx, &Call{
deploymentName: route.Deployment,
requestName: requestName,
startTime: start,
destVerb: verbRef,
callers: callers,
callError: types.Nil(err),
callError: optional.Nil(err),
request: req.Msg,
response: maybeResponse,
})
Expand Down Expand Up @@ -996,7 +996,7 @@ func (s *Service) watchModuleChanges(ctx context.Context, sendChange func(respon
initialCount := len(seedDeployments)
deploymentChanges := make(chan dal.DeploymentNotification, len(seedDeployments))
for _, deployment := range seedDeployments {
deploymentChanges <- dal.DeploymentNotification{Message: types.Some(deployment)}
deploymentChanges <- dal.DeploymentNotification{Message: optional.Some(deployment)}
}
logger.Infof("Seeded %d deployments", initialCount)

Expand Down
46 changes: 23 additions & 23 deletions backend/controller/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/alecthomas/types"
"github.com/alecthomas/types/optional"
sets "github.com/deckarep/golang-set/v2"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
Expand Down Expand Up @@ -84,9 +84,9 @@ func DeploymentArtefactFromProto(in *ftlv1.DeploymentArtefact) (DeploymentArtefa
}

func runnerFromDB(row sql.GetRunnerRow) Runner {
var deployment types.Option[model.DeploymentName]
var deployment optional.Option[model.DeploymentName]
if name, ok := row.DeploymentName.Get(); ok {
deployment = types.Some(model.DeploymentName(name))
deployment = optional.Some(model.DeploymentName(name))
}
attrs := model.Labels{}
if err := json.Unmarshal(row.Labels, &attrs); err != nil {
Expand All @@ -105,10 +105,10 @@ type Runner struct {
Key model.RunnerKey
Endpoint string
State RunnerState
ReservationTimeout types.Option[time.Duration]
Module types.Option[string]
ReservationTimeout optional.Option[time.Duration]
Module optional.Option[string]
// Assigned deployment key, if any.
Deployment types.Option[model.DeploymentName]
Deployment optional.Option[model.DeploymentName]
Labels model.Labels
}

Expand Down Expand Up @@ -309,9 +309,9 @@ func (d *DAL) GetStatus(
return Status{}, err
}
domainRunners, err := slices.MapErr(runners, func(in sql.GetActiveRunnersRow) (Runner, error) {
var deployment types.Option[model.DeploymentName]
var deployment optional.Option[model.DeploymentName]
if name, ok := in.DeploymentName.Get(); ok {
deployment = types.Some(model.DeploymentName(name))
deployment = optional.Some(model.DeploymentName(name))
}
attrs := model.Labels{}
if err := json.Unmarshal(in.Labels, &attrs); err != nil {
Expand Down Expand Up @@ -367,7 +367,7 @@ func (d *DAL) GetRunnersForDeployment(ctx context.Context, deployment model.Depl
Key: model.RunnerKey(row.Key),
Endpoint: row.Endpoint,
State: RunnerState(row.State),
Deployment: types.Some(deployment),
Deployment: optional.Some(deployment),
Labels: attrs,
})
}
Expand Down Expand Up @@ -507,9 +507,9 @@ func (d *DAL) GetDeployment(ctx context.Context, name model.DeploymentName) (*mo
// ErrConflict will be returned if a runner with the same endpoint and a
// different key already exists.
func (d *DAL) UpsertRunner(ctx context.Context, runner Runner) error {
var pgDeploymentName types.Option[string]
var pgDeploymentName optional.Option[string]
if dkey, ok := runner.Deployment.Get(); ok {
pgDeploymentName = types.Some(dkey.String())
pgDeploymentName = optional.Some(dkey.String())
}
attrBytes, err := json.Marshal(runner.Labels)
if err != nil {
Expand Down Expand Up @@ -595,7 +595,7 @@ func (d *DAL) ReserveRunnerForDeployment(ctx context.Context, deployment model.D
Key: model.RunnerKey(runner.Key),
Endpoint: runner.Endpoint,
State: RunnerState(runner.State),
Deployment: types.Some(deployment),
Deployment: optional.Some(deployment),
Labels: runnerLabels,
},
}, nil
Expand Down Expand Up @@ -667,7 +667,7 @@ func (d *DAL) ReplaceDeployment(ctx context.Context, newDeploymentName model.Dep
return translatePGError(err)
}

var replacedDeployment types.Option[string]
var replacedDeployment optional.Option[string]

// If there's an existing deployment, set its desired replicas to 0
oldDeployment, err := tx.GetExistingDeploymentForModule(ctx, newDeployment.ModuleName)
Expand All @@ -679,7 +679,7 @@ func (d *DAL) ReplaceDeployment(ctx context.Context, newDeploymentName model.Dep
if count == 1 {
return fmt.Errorf("%s: %w", "deployment already exists", ErrConflict)
}
replacedDeployment = types.Some(oldDeployment.Name.String())
replacedDeployment = optional.Some(oldDeployment.Name.String())
} else if !isNotFound(err) {
return translatePGError(err)
} else {
Expand Down Expand Up @@ -782,7 +782,7 @@ type Process struct {
Deployment model.DeploymentName
MinReplicas int
Labels model.Labels
Runner types.Option[ProcessRunner]
Runner optional.Option[ProcessRunner]
}

// GetProcessList returns a list of all "processes".
Expand All @@ -792,13 +792,13 @@ func (d *DAL) GetProcessList(ctx context.Context) ([]Process, error) {
return nil, translatePGError(err)
}
return slices.MapErr(rows, func(row sql.GetProcessListRow) (Process, error) {
var runner types.Option[ProcessRunner]
var runner optional.Option[ProcessRunner]
if endpoint, ok := row.Endpoint.Get(); ok {
var labels model.Labels
if err := json.Unmarshal(row.RunnerLabels, &labels); err != nil {
return Process{}, fmt.Errorf("invalid labels JSON for runner %s: %w", row.RunnerKey, err)
}
runner = types.Some(ProcessRunner{
runner = optional.Some(ProcessRunner{
Key: model.RunnerKey(row.RunnerKey.MustGet()),
Endpoint: endpoint,
Labels: labels,
Expand Down Expand Up @@ -905,9 +905,9 @@ func (d *DAL) InsertLogEvent(ctx context.Context, log *LogEvent) error {
if err != nil {
return err
}
var requestName types.Option[string]
var requestName optional.Option[string]
if name, ok := log.RequestName.Get(); ok {
requestName = types.Some(string(name))
requestName = optional.Some(string(name))
}
return translatePGError(d.db.InsertLogEvent(ctx, sql.InsertLogEventParams{
DeploymentName: log.DeploymentName,
Expand Down Expand Up @@ -984,13 +984,13 @@ func (d *DAL) UpsertController(ctx context.Context, key model.ControllerKey, add
}

func (d *DAL) InsertCallEvent(ctx context.Context, call *CallEvent) error {
var sourceModule, sourceVerb types.Option[string]
var sourceModule, sourceVerb optional.Option[string]
if sr, ok := call.SourceVerb.Get(); ok {
sourceModule, sourceVerb = types.Some(sr.Module), types.Some(sr.Name)
sourceModule, sourceVerb = optional.Some(sr.Module), optional.Some(sr.Name)
}
var requestName types.Option[string]
var requestName optional.Option[string]
if rn, ok := call.RequestName.Get(); ok {
requestName = types.Some(string(rn))
requestName = optional.Some(string(rn))
}
return translatePGError(d.db.InsertCallEvent(ctx, sql.InsertCallEventParams{
DeploymentName: call.DeploymentName.String(),
Expand Down
19 changes: 10 additions & 9 deletions backend/controller/dal/dal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"testing"
"time"

"github.com/alecthomas/assert/v2"
"github.com/alecthomas/types/optional"

"github.com/TBD54566975/ftl/backend/common/log"
"github.com/TBD54566975/ftl/backend/common/model"
"github.com/TBD54566975/ftl/backend/common/sha256"
"github.com/TBD54566975/ftl/backend/controller/sql/sqltest"
"github.com/TBD54566975/ftl/backend/schema"
ftlv1 "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1"
"github.com/alecthomas/assert/v2"
"github.com/alecthomas/types"
)

//nolint:maintidx
Expand Down Expand Up @@ -128,7 +129,7 @@ func TestDAL(t *testing.T) {
Labels: labels,
Endpoint: "http://localhost:8080",
State: RunnerStateReserved,
Deployment: types.Some(deploymentName),
Deployment: optional.Some(deploymentName),
}

t.Run("GetDeploymentsNeedingReconciliation", func(t *testing.T) {
Expand Down Expand Up @@ -190,7 +191,7 @@ func TestDAL(t *testing.T) {
Labels: labels,
Endpoint: "http://localhost:8080",
State: RunnerStateAssigned,
Deployment: types.Some(deploymentName),
Deployment: optional.Some(deploymentName),
})
assert.NoError(t, err)
})
Expand All @@ -209,7 +210,7 @@ func TestDAL(t *testing.T) {
Labels: labels,
Endpoint: "http://localhost:8080",
State: RunnerStateAssigned,
Deployment: types.Some(deploymentName),
Deployment: optional.Some(deploymentName),
}}, runners)
})

Expand All @@ -222,7 +223,7 @@ func TestDAL(t *testing.T) {
callEvent := &CallEvent{
Time: time.Now().Round(time.Millisecond),
DeploymentName: deploymentName,
RequestName: types.Some(requestName),
RequestName: optional.Some(requestName),
Request: []byte("{}"),
Response: []byte(`{"time": "now"}`),
DestVerb: schema.VerbRef{Module: "time", Name: "time"},
Expand All @@ -235,7 +236,7 @@ func TestDAL(t *testing.T) {
logEvent := &LogEvent{
Time: time.Now().Round(time.Millisecond),
DeploymentName: deploymentName,
RequestName: types.Some(requestName),
RequestName: optional.Some(requestName),
Level: int32(log.Warn),
Attributes: map[string]string{"attr": "value"},
Message: "A log entry",
Expand Down Expand Up @@ -270,7 +271,7 @@ func TestDAL(t *testing.T) {
})

t.Run("ByCall", func(t *testing.T) {
events, err := dal.QueryEvents(ctx, 1000, FilterTypes(EventTypeCall), FilterCall(types.None[string](), "time", types.None[string]()))
events, err := dal.QueryEvents(ctx, 1000, FilterTypes(EventTypeCall), FilterCall(optional.None[string](), "time", optional.None[string]()))
assert.NoError(t, err)
assertEventsEqual(t, []Event{callEvent}, events)
})
Expand Down Expand Up @@ -299,7 +300,7 @@ func TestDAL(t *testing.T) {
Labels: labels,
Endpoint: "http://localhost:8080",
State: RunnerStateAssigned,
Deployment: types.Some(model.NewDeploymentName("test")),
Deployment: optional.Some(model.NewDeploymentName("test")),
})
assert.Error(t, err)
assert.IsError(t, err, ErrNotFound)
Expand Down
Loading

0 comments on commit 4769934

Please sign in to comment.