github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/development/wasm/operations.go (about) 1 //go:build wasm 2 // +build wasm 3 4 package main 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/authzed/spicedb/pkg/development" 11 core "github.com/authzed/spicedb/pkg/proto/core/v1" 12 devinterface "github.com/authzed/spicedb/pkg/proto/developer/v1" 13 v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1" 14 "github.com/authzed/spicedb/pkg/schemadsl/generator" 15 "github.com/authzed/spicedb/pkg/tuple" 16 ) 17 18 func runOperation(devContext *development.DevContext, operation *devinterface.Operation) (*devinterface.OperationResult, error) { 19 switch { 20 case operation.FormatSchemaParameters != nil: 21 formatted, _, err := generator.GenerateSchema(devContext.CompiledSchema.OrderedDefinitions) 22 if err != nil { 23 return nil, err 24 } 25 26 trimmed := strings.TrimSpace(formatted) 27 return &devinterface.OperationResult{ 28 FormatSchemaResult: &devinterface.FormatSchemaResult{ 29 FormattedSchema: trimmed, 30 }, 31 }, nil 32 33 case operation.CheckParameters != nil: 34 var caveatContext map[string]any 35 if operation.CheckParameters.CaveatContext != nil { 36 caveatContext = operation.CheckParameters.CaveatContext.AsMap() 37 } 38 39 cr, err := development.RunCheck( 40 devContext, 41 operation.CheckParameters.Resource, 42 operation.CheckParameters.Subject, 43 caveatContext, 44 ) 45 if err != nil { 46 devErr, wireErr := development.DistinguishGraphError( 47 devContext, 48 err, 49 devinterface.DeveloperError_CHECK_WATCH, 50 0, 0, 51 tuple.MustString(&core.RelationTuple{ 52 ResourceAndRelation: operation.CheckParameters.Resource, 53 Subject: operation.CheckParameters.Subject, 54 }), 55 ) 56 if wireErr != nil { 57 return nil, wireErr 58 } 59 60 return &devinterface.OperationResult{ 61 CheckResult: &devinterface.CheckOperationsResult{ 62 CheckError: devErr, 63 }, 64 }, nil 65 } 66 67 membership := devinterface.CheckOperationsResult_NOT_MEMBER 68 if cr.Permissionship == v1.ResourceCheckResult_MEMBER { 69 membership = devinterface.CheckOperationsResult_MEMBER 70 } else if cr.Permissionship == v1.ResourceCheckResult_CAVEATED_MEMBER { 71 membership = devinterface.CheckOperationsResult_CAVEATED_MEMBER 72 } 73 74 return &devinterface.OperationResult{ 75 CheckResult: &devinterface.CheckOperationsResult{ 76 Membership: membership, 77 DebugInformation: cr.DispatchDebugInfo, 78 ResolvedDebugInformation: cr.V1DebugInfo, 79 PartialCaveatInfo: &devinterface.PartialCaveatInfo{ 80 MissingRequiredContext: cr.MissingCaveatFields, 81 }, 82 }, 83 }, nil 84 85 case operation.AssertionsParameters != nil: 86 assertions, devErr := development.ParseAssertionsYAML(operation.AssertionsParameters.AssertionsYaml) 87 if devErr != nil { 88 return &devinterface.OperationResult{ 89 AssertionsResult: &devinterface.RunAssertionsResult{ 90 InputError: devErr, 91 }, 92 }, nil 93 } 94 95 validationErrors, err := development.RunAllAssertions(devContext, assertions) 96 if err != nil { 97 return nil, err 98 } 99 100 return &devinterface.OperationResult{ 101 AssertionsResult: &devinterface.RunAssertionsResult{ 102 ValidationErrors: validationErrors, 103 }, 104 }, nil 105 106 case operation.ValidationParameters != nil: 107 validation, devErr := development.ParseExpectedRelationsYAML(operation.ValidationParameters.ValidationYaml) 108 if devErr != nil { 109 return &devinterface.OperationResult{ 110 ValidationResult: &devinterface.RunValidationResult{ 111 InputError: devErr, 112 }, 113 }, nil 114 } 115 116 membershipSet, validationErrors, err := development.RunValidation(devContext, validation) 117 if err != nil { 118 return nil, err 119 } 120 121 updatedValidationYaml := "" 122 if membershipSet != nil { 123 generatedValidationYaml, gerr := development.GenerateValidation(membershipSet) 124 if gerr != nil { 125 return nil, gerr 126 } 127 updatedValidationYaml = generatedValidationYaml 128 } 129 130 return &devinterface.OperationResult{ 131 ValidationResult: &devinterface.RunValidationResult{ 132 UpdatedValidationYaml: updatedValidationYaml, 133 ValidationErrors: validationErrors, 134 }, 135 }, nil 136 137 default: 138 return nil, fmt.Errorf("unknown operation") 139 } 140 }