From dc5852f9afb153d6e6d4d5e9b68a1ae89cde3a33 Mon Sep 17 00:00:00 2001 From: Kartikay Date: Tue, 14 Jan 2025 20:33:12 +0530 Subject: [PATCH] merge loops and enhance logic Signed-off-by: Kartikay --- go.mod | 2 + go.sum | 4 +- internal/cmd/validate.go | 85 +++++++++++++++++--------------------- internal/decode/decoder.go | 5 ++- 4 files changed, 46 insertions(+), 50 deletions(-) diff --git a/go.mod b/go.mod index 97fb4e0..c4c96cb 100644 --- a/go.mod +++ b/go.mod @@ -261,3 +261,5 @@ require ( sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect sigs.k8s.io/yaml v1.4.0 // indirect ) + +replace github.com/authzed/spicedb => github.com/kartikaysaxena/spicedb v0.0.0-20250113154244-813e62735279 diff --git a/go.sum b/go.sum index 488217c..d470ccb 100644 --- a/go.sum +++ b/go.sum @@ -678,8 +678,6 @@ github.com/authzed/consistent v0.1.0 h1:tlh1wvKoRbjRhMm2P+X5WQQyR54SRoS4MyjLOg17 github.com/authzed/consistent v0.1.0/go.mod h1:plwHlrN/EJUCwQ+Bca0MhM1KnisPs7HEkZI5giCXrcc= github.com/authzed/grpcutil v0.0.0-20240123194739-2ea1e3d2d98b h1:wbh8IK+aMLTCey9sZasO7b6BWLAJnHHvb79fvWCXwxw= github.com/authzed/grpcutil v0.0.0-20240123194739-2ea1e3d2d98b/go.mod h1:s3qC7V7XIbiNWERv7Lfljy/Lx25/V1Qlexb0WJuA8uQ= -github.com/authzed/spicedb v1.39.1-0.20250108165209-c18b1656bdd0 h1:ewOiKCJmuLU7/+HyUrJD/oIMgd7NG0NrpHfl4nzeW/s= -github.com/authzed/spicedb v1.39.1-0.20250108165209-c18b1656bdd0/go.mod h1:/UVC4ZJkMUZFN4MVjjOLAU7m/fqitkBP57ZPffyicOs= github.com/aws/aws-sdk-go-v2 v1.32.7 h1:ky5o35oENWi0JYWUZkB7WYvVPP+bcRF5/Iq7JWSb5Rw= github.com/aws/aws-sdk-go-v2 v1.32.7/go.mod h1:P5WJBrYqqbWVaOxgH0X/FYYD47/nooaPOZPlQdmiN2U= github.com/aws/aws-sdk-go-v2/config v1.28.7 h1:GduUnoTXlhkgnxTD93g1nv4tVPILbdNQOzav+Wpg7AE= @@ -1119,6 +1117,8 @@ github.com/jzelinskie/cobrautil/v2 v2.0.0-20240819150235-f7fe73942d0f h1:+WgAZQQ github.com/jzelinskie/cobrautil/v2 v2.0.0-20240819150235-f7fe73942d0f/go.mod h1:jsl6cEF6BT3UeQoSLreA7G0sZXemoI5XNqyxzWCohbE= github.com/jzelinskie/stringz v0.0.3 h1:0GhG3lVMYrYtIvRbxvQI6zqRTT1P1xyQlpa0FhfUXas= github.com/jzelinskie/stringz v0.0.3/go.mod h1:hHYbgxJuNLRw91CmpuFsYEOyQqpDVFg8pvEh23vy4P0= +github.com/kartikaysaxena/spicedb v0.0.0-20250113154244-813e62735279 h1:rX8BE4sozX/QQ+ViPUohSf8kPGn61zEsgT7bS8OKV1Q= +github.com/kartikaysaxena/spicedb v0.0.0-20250113154244-813e62735279/go.mod h1:/UVC4ZJkMUZFN4MVjjOLAU7m/fqitkBP57ZPffyicOs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= diff --git a/internal/cmd/validate.go b/internal/cmd/validate.go index 53f4f82..8f77a84 100644 --- a/internal/cmd/validate.go +++ b/internal/cmd/validate.go @@ -93,8 +93,8 @@ func validateCmdFunc(cmd *cobra.Command, args []string) error { // Initialize variables for multiple files var ( onlySchemaFiles = true - validateContentsFiles [][]byte - parsedFiles []validationfile.ValidationFile + validateContentsFiles = make([][]byte, 0, len(args)) + parsedFiles = make([]validationfile.ValidationFile, 0, len(args)) totalAssertions int totalRelationsValidated int totalFiles = len(args) @@ -132,31 +132,30 @@ func validateCmdFunc(cmd *cobra.Command, args []string) error { // Create the development context for all parsed files ctx := cmd.Context() tuples := make([]*core.RelationTuple, 0) + for _, parsed := range parsedFiles { for _, rel := range parsed.Relationships.Relationships { tuples = append(tuples, rel.ToCoreTuple()) } - } - devCtx, devErrs, err := development.NewDevContext(ctx, &devinterface.RequestContext{ - Schema: parsedFiles[0].Schema.Schema, - Relationships: tuples, - }) - if err != nil { - return err - } - if devErrs != nil { - schemaOffset := 1 /* for the 'schema:' */ - if onlySchemaFiles { - schemaOffset = 0 + devCtx, devErrs, err := development.NewDevContext(ctx, &devinterface.RequestContext{ + Schema: parsed.Schema.Schema, + Relationships: tuples, + }) + if err != nil { + return err } + if devErrs != nil { + schemaOffset := 1 /* for the 'schema:' */ + if onlySchemaFiles { + schemaOffset = 0 + } - // Output errors for all files - for _, validateContents := range validateContentsFiles { - outputDeveloperErrorsWithLineOffset(validateContents, devErrs.InputErrors, schemaOffset) + // Output errors for all files + for _, validateContents := range validateContentsFiles { + outputDeveloperErrorsWithLineOffset(validateContents, devErrs.InputErrors, schemaOffset) + } } - } - // Run assertions for all parsed files - for _, parsed := range parsedFiles { + // Run assertions for all parsed files adevErrs, aerr := development.RunAllAssertions(devCtx, &parsed.Assertions) if aerr != nil { return aerr @@ -167,9 +166,8 @@ func validateCmdFunc(cmd *cobra.Command, args []string) error { } } successfullyValidatedFiles++ - } - // Run expected relations for all parsed files - for _, parsed := range parsedFiles { + + // Run expected relations for all parsed files _, erDevErrs, rerr := development.RunValidation(devCtx, &parsed.ExpectedRelations) if rerr != nil { return rerr @@ -179,31 +177,26 @@ func validateCmdFunc(cmd *cobra.Command, args []string) error { outputDeveloperErrors(validateContents, erDevErrs) } } - } - - // Print out any warnings for all files - warnings, err := development.GetWarnings(ctx, devCtx) - if err != nil { - return err - } - - if len(warnings) > 0 { - for _, warning := range warnings { - console.Printf("%s%s\n", warningPrefix(), warning.Message) - for _, validateContents := range validateContentsFiles { - outputForLine(validateContents, uint64(warning.Line), warning.SourceCode, uint64(warning.Column)) // warning.LineNumber is 1-indexed - } - console.Printf("\n") + // Print out any warnings for all files + warnings, err := development.GetWarnings(ctx, devCtx) + if err != nil { + return err } + if len(warnings) > 0 { + for _, warning := range warnings { + console.Printf("%s%s\n", warningPrefix(), warning.Message) + for _, validateContents := range validateContentsFiles { + outputForLine(validateContents, uint64(warning.Line), warning.SourceCode, uint64(warning.Column)) // warning.LineNumber is 1-indexed + } + console.Printf("\n") + } - console.Print(complete()) - } else { - console.Print(success()) - } - - for _, parsedFile := range parsedFiles { - totalAssertions += len(parsedFile.Assertions.AssertTrue) + len(parsedFile.Assertions.AssertFalse) - totalRelationsValidated += len(parsedFile.ExpectedRelations.ValidationMap) + console.Print(complete()) + } else { + console.Print(success()) + } + totalAssertions += len(parsed.Assertions.AssertTrue) + len(parsed.Assertions.AssertFalse) + totalRelationsValidated += len(parsed.ExpectedRelations.ValidationMap) } console.Printf(" - %d relationships loaded, %d assertions run, %d expected relations validated\n", diff --git a/internal/decode/decoder.go b/internal/decode/decoder.go index fd98e71..19be210 100644 --- a/internal/decode/decoder.go +++ b/internal/decode/decoder.go @@ -107,13 +107,14 @@ func directHTTPDecoder(u *url.URL) Func { // Uses the files passed in the args and looks for the specified schemaFile to parse the YAML. func unmarshalAsYAMLOrSchemaWithFile(data []byte, out interface{}, args []string) (bool, error) { - if strings.Contains(string(data), "schemaFile:") { + if (strings.Contains(string(data), "schemaFile:") && !strings.Contains(string(data), "schema:")) { if err := yaml.Unmarshal(data, out); err != nil { return false, err } schema := out.(*validationfile.ValidationFile) for _, arg := range args { + // Check if the file specified in schemaFile is passed as an arguement. if strings.Contains(arg, schema.SchemaFile) { file, err := os.Open(arg) if err != nil { @@ -131,7 +132,7 @@ func unmarshalAsYAMLOrSchemaWithFile(data []byte, out interface{}, args []string func unmarshalAsYAMLOrSchema(data []byte, out interface{}) (bool, error) { // Check for indications of a schema-only file. - if !strings.Contains(string(data), "schema:") { + if (!strings.Contains(string(data), "schema:") && !strings.Contains(string(data), "relationships:")) { compiled, serr := compiler.Compile(compiler.InputSchema{ Source: input.Source("schema"), SchemaString: string(data),