Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions internal/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ func registerImportCmd(rootCmd *cobra.Command) {
if err != nil {
return err
}
prefix, err := determinePrefixForSchema(cmd.Context(), cobrautil.MustGetString(cmd, "schema-definition-prefix"), client, nil)
if err != nil {
return err
}
prefix := cobrautil.MustGetString(cmd, "schema-definition-prefix")
log.Trace().Msgf("using prefix: %s", prefix)
return importCmdFunc(cmd, client, client, prefix, args[0])
},
Expand Down
29 changes: 6 additions & 23 deletions internal/cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func schemaCopyInner(ctx context.Context, srcClient, destClient v1.SchemaService
}
log.Trace().Interface("response", readResp).Msg("read schema")

prefix, err := determinePrefixForSchema(ctx, definitionPrefix, nil, &readResp.SchemaText)
prefix, err := determinePrefixForSchema(definitionPrefix, readResp.SchemaText)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -284,12 +284,7 @@ func schemaWriteCmdImpl(cmd *cobra.Command, args []string, client v1.SchemaServi
return errors.New("attempted to write empty schema")
}

prefix, err := determinePrefixForSchema(cmd.Context(), cobrautil.MustGetString(cmd, "schema-definition-prefix"), client, nil)
if err != nil {
return err
}

schemaText, err := rewriteSchema(cmd.Context(), string(schemaBytes), prefix)
schemaText, err := rewriteSchema(cmd.Context(), string(schemaBytes), cobrautil.MustGetString(cmd, "schema-definition-prefix"))
if err != nil {
return err
}
Expand Down Expand Up @@ -337,32 +332,20 @@ func rewriteSchema(ctx context.Context, existingSchemaText string, definitionPre
// determinePrefixForSchema determines the prefix to be applied to a schema that will be written.
//
// If specifiedPrefix is non-empty, it is returned immediately.
// If existingSchema is non-nil, it is parsed for the prefix.
// Otherwise, the client is used to retrieve the existing schema (if any), and the prefix is retrieved from there.
func determinePrefixForSchema(ctx context.Context, specifiedPrefix string, client v1.SchemaServiceClient, existingSchema *string) (string, error) {
// Otherwise, the existing schema is parsed for the prefix.
func determinePrefixForSchema(specifiedPrefix string, existingSchema string) (string, error) {
if specifiedPrefix != "" {
return specifiedPrefix, nil
}

var schemaText string
if existingSchema != nil {
schemaText = *existingSchema
} else {
readSchemaText, err := commands.ReadSchema(ctx, client)
if err != nil {
return "", nil
}
schemaText = readSchemaText
}

// If there is no schema found, return the empty string.
if schemaText == "" {
if existingSchema == "" {
return "", nil
}

// Otherwise, compile the schema and grab the prefixes of the namespaces defined.
found, err := compiler.Compile(
compiler.InputSchema{Source: input.Source("schema"), SchemaString: schemaText},
compiler.InputSchema{Source: input.Source("schema"), SchemaString: existingSchema},
compiler.AllowUnprefixedObjectType(),
compiler.SkipValidation(),
)
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestDeterminePrefixForSchema(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
found, err := determinePrefixForSchema(t.Context(), test.specifiedPrefix, nil, &test.existingSchema)
found, err := determinePrefixForSchema(test.specifiedPrefix, test.existingSchema)
require.NoError(t, err)
require.Equal(t, test.expectedPrefix, found)
})
Expand Down Expand Up @@ -450,11 +450,11 @@ definition resource {
defer ctrl.Finish()
mockClient := NewMockSchemaServiceClient(ctrl)

// ReadSchema is always called at least once
// ReadSchema must never be called: the schema is written as given,
// without inferring a definition prefix from the existing schema.
mockClient.EXPECT().
ReadSchema(gomock.Any(), gomock.Any()).
Return(&v1.ReadSchemaResponse{SchemaText: ""}, nil).
MaxTimes(2) // sometimes we read for prefix determination
Times(0)

// Set up WriteSchema expectations based on test case
var receivedSchema string
Expand Down
23 changes: 0 additions & 23 deletions internal/commands/schema.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package commands

import (
"context"

"github.com/jzelinskie/cobrautil/v2"
"github.com/jzelinskie/stringz"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"

Expand Down Expand Up @@ -63,22 +59,3 @@ func schemaReadCmdFunc(cmd *cobra.Command, _ []string) error {
console.Println(stringz.Join("\n\n", resp.SchemaText))
return nil
}

// ReadSchema calls read schema for the client and returns the schema found.
func ReadSchema(ctx context.Context, client v1.SchemaServiceClient) (string, error) {
request := &v1.ReadSchemaRequest{}
log.Trace().Interface("request", request).Msg("requesting schema read")

resp, err := client.ReadSchema(ctx, request)
if err != nil {
errStatus, ok := status.FromError(err)
if !ok || errStatus.Code() != codes.NotFound {
return "", err
}

log.Debug().Msg("no schema defined")
return "", nil
}

return resp.SchemaText, nil
}
Loading