Skip to content

Commit 7f9bd27

Browse files
authored
build: add new mustcallcheck analyzer and fix instances (#2886)
1 parent 3b519e8 commit 7f9bd27

File tree

18 files changed

+532
-22
lines changed

18 files changed

+532
-22
lines changed

internal/datasets/subjectsetbytype.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ func (s *SubjectByTypeSet) ForEachType(handler func(rr *core.RelationReference,
5858
func (s *SubjectByTypeSet) Map(mapper func(rr *core.RelationReference) (*core.RelationReference, error)) (*SubjectByTypeSet, error) {
5959
mapped := NewSubjectByTypeSet()
6060
for key, subjectset := range s.byType {
61-
ns, rel := tuple.MustSplitRelRef(key)
61+
ns, rel, err := tuple.SplitRelRef(key)
62+
if err != nil {
63+
return nil, err
64+
}
6265
updatedType, err := mapper(&core.RelationReference{
6366
Namespace: ns,
6467
Relation: rel,

internal/datastore/crdb/schema/indexutil.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ func mustInit() {
2727
}
2828
}
2929

30+
var indexColumnRegex = regexp.MustCompile(`\(([^)]+)\)`)
31+
3032
func parseIndexColumns(columnsSQL string) ([]string, error) {
3133
// Match columns within parentheses, handling both PRIMARY KEY and table_name formats
32-
re := regexp.MustCompile(`\(([^)]+)\)`)
33-
matches := re.FindStringSubmatch(columnsSQL)
34+
matches := indexColumnRegex.FindStringSubmatch(columnsSQL)
3435
if len(matches) < 2 {
3536
return nil, fmt.Errorf("no columns found in parentheses in SQL: %s", columnsSQL)
3637
}

internal/datastore/memdb/readwrite.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,15 @@ func (rwt *memdbReadWriteTx) write(tx *memdb.Txn, mutations ...tuple.Relationshi
103103
if err != nil {
104104
return err
105105
}
106-
if tuple.MustString(rt) == tuple.MustString(mutation.Relationship) {
106+
existingRelationshipString, err := tuple.String(rt)
107+
if err != nil {
108+
return err
109+
}
110+
mutationRelationshipString, err := tuple.String(mutation.Relationship)
111+
if err != nil {
112+
return err
113+
}
114+
if existingRelationshipString == mutationRelationshipString {
107115
continue
108116
}
109117
}

magefiles/lint.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ func (Lint) Analyzers() error {
9393
"-exprstatementcheck.disallowed-expr-statement-types=*github.com/rs/zerolog.Event:MarshalZerologObject:missing Send or Msg on zerolog log Event",
9494
"-paniccheck",
9595
"-paniccheck.skip-files=_test,zz_,migrations/index.go",
96+
"-mustcallcheck",
97+
"-mustcallcheck.skip-files=_test,zz_",
9698
"-zerologmarshalcheck",
9799
"-zerologmarshalcheck.skip-files=_test,zz_",
98100
"-protomarshalcheck",

pkg/cmd/serve.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,22 @@ func RegisterServeFlags(cmd *cobra.Command, config *server.Config) error {
135135
return fmt.Errorf("failed to mark flag as hidden: %w", err)
136136
}
137137
namespaceCacheFlags.DurationVar(&config.SchemaWatchHeartbeat, "datastore-schema-watch-heartbeat", 1*time.Second, "heartbeat time on the schema watch in the datastore (if supported). 0 means to default to the datastore's minimum.")
138-
server.MustRegisterCacheFlags(namespaceCacheFlags, "ns-cache", "schema", &config.NamespaceCacheConfig, namespaceCacheDefaults)
138+
err := server.RegisterCacheFlags(namespaceCacheFlags, "ns-cache", "schema", &config.NamespaceCacheConfig, namespaceCacheDefaults)
139+
if err != nil {
140+
return fmt.Errorf("could not register namespace cache flags: %w", err)
141+
}
139142

140143
dispatchFlags := nfs.FlagSet(BoldBlue("Dispatch"))
141144
// Flags for configuring the dispatch server
142145
util.RegisterGRPCServerFlags(dispatchFlags, &config.DispatchServer, "dispatch-cluster", "dispatch", ":50053", false)
143-
server.MustRegisterCacheFlags(dispatchFlags, "dispatch-cache", "dispatch calls this server makes to other servers", &config.DispatchCacheConfig, dispatchCacheDefaults)
144-
server.MustRegisterCacheFlags(dispatchFlags, "dispatch-cluster-cache", "dispatch calls this server receives from other servers", &config.ClusterDispatchCacheConfig, dispatchClusterCacheDefaults)
146+
err = server.RegisterCacheFlags(dispatchFlags, "dispatch-cache", "dispatch calls this server makes to other servers", &config.DispatchCacheConfig, dispatchCacheDefaults)
147+
if err != nil {
148+
return fmt.Errorf("could not register dispatch cache flags: %w", err)
149+
}
150+
err = server.RegisterCacheFlags(dispatchFlags, "dispatch-cluster-cache", "dispatch calls this server receives from other servers", &config.ClusterDispatchCacheConfig, dispatchClusterCacheDefaults)
151+
if err != nil {
152+
return fmt.Errorf("could not register dispatch cluster cache flags: %w", err)
153+
}
145154

146155
// Flags for configuring dispatch requests
147156
dispatchFlags.Uint16Var(&config.DispatchChunkSize, "dispatch-chunk-size", 100, "maximum number of object IDs in a dispatched request")
@@ -188,7 +197,10 @@ func RegisterServeFlags(cmd *cobra.Command, config *server.Config) error {
188197
return fmt.Errorf("failed to mark flag as deprecated: %w", err)
189198
}
190199

191-
server.MustRegisterCacheFlags(experimentalFlags, "lookup-resources-chunk-cache", "LookupResources3 chunks", &config.LR3ResourceChunkCacheConfig, lr3ChunkCacheDefaults)
200+
err = server.RegisterCacheFlags(experimentalFlags, "lookup-resources-chunk-cache", "LookupResources3 chunks", &config.LR3ResourceChunkCacheConfig, lr3ChunkCacheDefaults)
201+
if err != nil {
202+
return fmt.Errorf("could not register lookup resources chunk cache flags: %w", err)
203+
}
192204

193205
tracingFlags := nfs.FlagSet(BoldBlue("Tracing"))
194206
// Flags for tracing

pkg/cmd/server/cacheconfig.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,8 @@ func parsePercent(str string, freeMem uint64) (uint64, error) {
137137
return freeMem / 100 * parsedPercent, nil
138138
}
139139

140-
// MustRegisterCacheFlags registers flags used to configure SpiceDB's various
141-
// caches.
142-
func MustRegisterCacheFlags(flags *pflag.FlagSet, flagPrefix, flagDescription string, config, defaults *CacheConfig) {
140+
// RegisterCacheFlags registers flags used to configure SpiceDB's various caches.
141+
func RegisterCacheFlags(flags *pflag.FlagSet, flagPrefix, flagDescription string, config, defaults *CacheConfig) error {
143142
config.Name = defaults.Name
144143
flagPrefix = cmp.Or(flagPrefix, "cache")
145144
flags.StringVar(&config.MaxCost, flagPrefix+"-max-cost", defaults.MaxCost, "upper bound (in bytes or as a percent of available memory) of the cache for "+flagDescription)
@@ -150,6 +149,7 @@ func MustRegisterCacheFlags(flags *pflag.FlagSet, flagPrefix, flagDescription st
150149
// Hidden flags.
151150
flags.StringVar(&config.CacheKindForTesting, flagPrefix+"-kind-for-testing", defaults.CacheKindForTesting, "choose a different kind of cache, for testing")
152151
if err := flags.MarkHidden(flagPrefix + "-kind-for-testing"); err != nil {
153-
panic(err)
152+
return err
154153
}
154+
return nil
155155
}

pkg/composableschemadsl/compiler/translator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ func translateExpressionOperationDirect(tctx *translationContext, expressionOpNo
610610
return nil, err
611611
}
612612

613-
return namespace.MustFunctionedTupleToUserset(tuplesetRelation, functionName, usersetRelation), nil
613+
return namespace.FunctionedTupleToUserset(tuplesetRelation, functionName, usersetRelation)
614614
}
615615

616616
return namespace.TupleToUserset(tuplesetRelation, usersetRelation), nil

pkg/development/devcontext.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,17 @@ func newDevContextWithDatastore(ctx context.Context, requestContext *devinterfac
148148
RelationshipChunkCache: nil, // Disable caching for devcontext
149149
}
150150

151+
dispatcher, err := graph.NewLocalOnlyDispatcher(params)
152+
if err != nil {
153+
return nil, nil, err
154+
}
155+
151156
return &DevContext{
152157
Ctx: ctx,
153158
Datastore: ds,
154159
CompiledSchema: compiled,
155160
Revision: currentRevision,
156-
Dispatcher: graph.MustNewLocalOnlyDispatcher(params),
161+
Dispatcher: dispatcher,
157162
}, nil, nil
158163
}
159164

pkg/namespace/builder.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func TupleToUserset(tuplesetRelation, usersetRelation string) *core.SetOperation
307307
// MustFunctionedTupleToUserset creates a child which first loads all tuples with the specific relation,
308308
// and then applies the function to all children on the usersets found by following a relation on those loaded
309309
// tuples.
310-
func MustFunctionedTupleToUserset(tuplesetRelation, functionName, usersetRelation string) *core.SetOperation_Child {
310+
func FunctionedTupleToUserset(tuplesetRelation, functionName, usersetRelation string) (*core.SetOperation_Child, error) {
311311
function := core.FunctionedTupleToUserset_FUNCTION_ANY
312312

313313
switch functionName {
@@ -318,7 +318,7 @@ func MustFunctionedTupleToUserset(tuplesetRelation, functionName, usersetRelatio
318318
function = core.FunctionedTupleToUserset_FUNCTION_ALL
319319

320320
default:
321-
panic(spiceerrors.MustBugf("unknown function name: %s", functionName))
321+
return nil, spiceerrors.MustBugf("unknown function name: %s", functionName)
322322
}
323323

324324
return &core.SetOperation_Child{
@@ -334,7 +334,17 @@ func MustFunctionedTupleToUserset(tuplesetRelation, functionName, usersetRelatio
334334
},
335335
},
336336
},
337+
}, nil
338+
}
339+
340+
// MustFunctionedTupleToUserset is a wrapper around FunctionedTupleToUserset that panics if an error
341+
// is encountered.
342+
func MustFunctionedTupleToUserset(tuplesetRelation, functionName, usersetRelation string) *core.SetOperation_Child {
343+
operation, err := FunctionedTupleToUserset(tuplesetRelation, functionName, usersetRelation)
344+
if err != nil {
345+
panic(err)
337346
}
347+
return operation
338348
}
339349

340350
// Rewrite wraps a rewrite as a set operation child of another rewrite.

pkg/schema/reachabilitygraph.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ func (rg *DefinitionReachability) RelationsEncounteredForResource(
4444

4545
relationRefs := make([]*core.RelationReference, 0, len(relationNames))
4646
for _, relationName := range relationNames {
47-
namespace, relation := tuple.MustSplitRelRef(relationName)
47+
namespace, relation, err := tuple.SplitRelRef(relationName)
48+
if err != nil {
49+
return nil, err
50+
}
4851
relationRefs = append(relationRefs, &core.RelationReference{
4952
Namespace: namespace,
5053
Relation: relation,
@@ -113,7 +116,10 @@ func (rg *DefinitionReachability) RelationsEncounteredForSubject(
113116

114117
relationRefs := make([]*core.RelationReference, 0, allRelationNames.Len())
115118
for _, relationName := range allRelationNames.AsSlice() {
116-
namespace, relation := tuple.MustSplitRelRef(relationName)
119+
namespace, relation, err := tuple.SplitRelRef(relationName)
120+
if err != nil {
121+
return nil, err
122+
}
117123
relationRefs = append(relationRefs, &core.RelationReference{
118124
Namespace: namespace,
119125
Relation: relation,

0 commit comments

Comments
 (0)