github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/development/parsing.go (about)

     1  package development
     2  
     3  import (
     4  	devinterface "github.com/authzed/spicedb/pkg/proto/developer/v1"
     5  	"github.com/authzed/spicedb/pkg/spiceerrors"
     6  	"github.com/authzed/spicedb/pkg/validationfile"
     7  	"github.com/authzed/spicedb/pkg/validationfile/blocks"
     8  )
     9  
    10  // ParseAssertionsYAML parses the YAML form of an assertions block.
    11  func ParseAssertionsYAML(assertionsYaml string) (*blocks.Assertions, *devinterface.DeveloperError) {
    12  	assertions, err := validationfile.ParseAssertionsBlock([]byte(assertionsYaml))
    13  	if err != nil {
    14  		serr, ok := spiceerrors.AsErrorWithSource(err)
    15  		if ok {
    16  			return nil, convertSourceError(devinterface.DeveloperError_ASSERTION, serr)
    17  		}
    18  	}
    19  
    20  	return assertions, convertError(devinterface.DeveloperError_ASSERTION, err)
    21  }
    22  
    23  // ParseExpectedRelationsYAML parses the YAML form of an expected relations block.
    24  func ParseExpectedRelationsYAML(expectedRelationsYaml string) (*blocks.ParsedExpectedRelations, *devinterface.DeveloperError) {
    25  	block, err := validationfile.ParseExpectedRelationsBlock([]byte(expectedRelationsYaml))
    26  	if err != nil {
    27  		serr, ok := spiceerrors.AsErrorWithSource(err)
    28  		if ok {
    29  			return nil, convertSourceError(devinterface.DeveloperError_VALIDATION_YAML, serr)
    30  		}
    31  	}
    32  	return block, convertError(devinterface.DeveloperError_VALIDATION_YAML, err)
    33  }
    34  
    35  func convertError(source devinterface.DeveloperError_Source, err error) *devinterface.DeveloperError {
    36  	if err == nil {
    37  		return nil
    38  	}
    39  
    40  	return &devinterface.DeveloperError{
    41  		Message: err.Error(),
    42  		Kind:    devinterface.DeveloperError_PARSE_ERROR,
    43  		Source:  source,
    44  		Line:    0,
    45  	}
    46  }
    47  
    48  func convertSourceError(source devinterface.DeveloperError_Source, err *spiceerrors.ErrorWithSource) *devinterface.DeveloperError {
    49  	return &devinterface.DeveloperError{
    50  		Message: err.Error(),
    51  		Kind:    devinterface.DeveloperError_PARSE_ERROR,
    52  		Source:  source,
    53  		Line:    uint32(err.LineNumber),
    54  		Column:  uint32(err.ColumnPosition),
    55  		Context: err.SourceCodeString,
    56  	}
    57  }