summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/pkg/development/parsing.go
blob: 75e27c8ff156c4eefe101f600ee621323cdaf009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package development

import (
	"github.com/ccoveille/go-safecast"

	log "github.com/authzed/spicedb/internal/logging"
	devinterface "github.com/authzed/spicedb/pkg/proto/developer/v1"
	"github.com/authzed/spicedb/pkg/spiceerrors"
	"github.com/authzed/spicedb/pkg/validationfile"
	"github.com/authzed/spicedb/pkg/validationfile/blocks"
)

// ParseAssertionsYAML parses the YAML form of an assertions block.
func ParseAssertionsYAML(assertionsYaml string) (*blocks.Assertions, *devinterface.DeveloperError) {
	assertions, err := validationfile.ParseAssertionsBlock([]byte(assertionsYaml))
	if err != nil {
		serr, ok := spiceerrors.AsWithSourceError(err)
		if ok {
			return nil, convertSourceError(devinterface.DeveloperError_ASSERTION, serr)
		}
	}

	return assertions, convertError(devinterface.DeveloperError_ASSERTION, err)
}

// ParseExpectedRelationsYAML parses the YAML form of an expected relations block.
func ParseExpectedRelationsYAML(expectedRelationsYaml string) (*blocks.ParsedExpectedRelations, *devinterface.DeveloperError) {
	block, err := validationfile.ParseExpectedRelationsBlock([]byte(expectedRelationsYaml))
	if err != nil {
		serr, ok := spiceerrors.AsWithSourceError(err)
		if ok {
			return nil, convertSourceError(devinterface.DeveloperError_VALIDATION_YAML, serr)
		}
	}
	return block, convertError(devinterface.DeveloperError_VALIDATION_YAML, err)
}

func convertError(source devinterface.DeveloperError_Source, err error) *devinterface.DeveloperError {
	if err == nil {
		return nil
	}

	return &devinterface.DeveloperError{
		Message: err.Error(),
		Kind:    devinterface.DeveloperError_PARSE_ERROR,
		Source:  source,
		Line:    0,
	}
}

func convertSourceError(source devinterface.DeveloperError_Source, err *spiceerrors.WithSourceError) *devinterface.DeveloperError {
	// NOTE: zeroes are fine here to mean "unknown"
	lineNumber, castErr := safecast.ToUint32(err.LineNumber)
	if castErr != nil {
		log.Err(castErr).Msg("could not cast lineNumber to uint32")
	}
	columnPosition, castErr := safecast.ToUint32(err.ColumnPosition)
	if castErr != nil {
		log.Err(castErr).Msg("could not cast columnPosition to uint32")
	}
	return &devinterface.DeveloperError{
		Message: err.Error(),
		Kind:    devinterface.DeveloperError_PARSE_ERROR,
		Source:  source,
		Line:    lineNumber,
		Column:  columnPosition,
		Context: err.SourceCodeString,
	}
}