Skip to content

Commit

Permalink
merge loops and enhance logic
Browse files Browse the repository at this point in the history
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
  • Loading branch information
kartikaysaxena committed Jan 14, 2025
1 parent 24fd2b1 commit dc5852f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 50 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
85 changes: 39 additions & 46 deletions internal/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions internal/decode/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand Down

0 comments on commit dc5852f

Please sign in to comment.