Skip to content

Commit

Permalink
chore(lint): fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh committed Jan 22, 2024
1 parent bfb718a commit d63889a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
67 changes: 44 additions & 23 deletions cmd/tuple/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"path"
"strings"

"github.com/openfga/cli/internal/clierrors"

openfga "github.com/openfga/go-sdk"
"github.com/openfga/go-sdk/client"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -183,12 +185,14 @@ func parseTuplesFromCSV(data []byte, tuples *[]client.ClientTupleKey) error {
if err != nil {
return err
}

for index := 0; true; index++ {
tuple, err := reader.Read()
if err != nil {
if errors.Is(err, io.EOF) {
break
}

return fmt.Errorf("failed to read tuple from csv file: %w", err)
}

Expand All @@ -197,22 +201,9 @@ func parseTuplesFromCSV(data []byte, tuples *[]client.ClientTupleKey) error {
tupleUserKey += "#" + tuple[columns.UserRelation]
}

var condition *openfga.RelationshipCondition

if columns.ConditionName != -1 && tuple[columns.ConditionName] != "" {
conditionContext := &(map[string]interface{}{})
if columns.ConditionContext != -1 {
conditionContext, err = cmdutils.ParseQueryContextInner(tuple[columns.ConditionContext])
if err != nil {
return fmt.Errorf("failed to read condition context on line %d: %w", index, err)
}

}

condition = &openfga.RelationshipCondition{
Name: tuple[columns.ConditionName],
Context: conditionContext,
}
condition, err := parseConditionColumnsForRow(columns, tuple, index)
if err != nil {
return err
}

tupleKey := client.ClientTupleKey{
Expand All @@ -228,6 +219,30 @@ func parseTuplesFromCSV(data []byte, tuples *[]client.ClientTupleKey) error {
return nil
}

func parseConditionColumnsForRow(columns *csvColumns, tuple []string, index int) (*openfga.RelationshipCondition, error) {
var condition *openfga.RelationshipCondition

if columns.ConditionName != -1 && tuple[columns.ConditionName] != "" {
conditionContext := &(map[string]interface{}{})

if columns.ConditionContext != -1 {
var err error

conditionContext, err = cmdutils.ParseQueryContextInner(tuple[columns.ConditionContext])
if err != nil {
return nil, fmt.Errorf("failed to read condition context on line %d: %w", index, err)
}
}

condition = &openfga.RelationshipCondition{
Name: tuple[columns.ConditionName],
Context: conditionContext,
}
}

return condition, nil
}

type csvColumns struct {
UserType int
UserID int
Expand Down Expand Up @@ -258,31 +273,37 @@ func (columns *csvColumns) setHeaderIndex(headerName string, index int) error {
case "condition_context":
columns.ConditionContext = index
default:
return fmt.Errorf("invalid header %q, valid headers are user_type,user_id,user_relation,relation,object_type,object_id,condition_name,condition_context", headerName)
return fmt.Errorf("invalid header %q, valid headers are user_type,user_id,user_relation,relation,object_type,object_id,condition_name,condition_context", headerName) //nolint:goerr113
}

return nil
}

func (columns *csvColumns) validate() error {
if columns.UserType == -1 {
return errors.New("required csv header \"user_type\" not found")
return clierrors.MissingRequiredCsvHeaderError("user_type") //nolint:wrapcheck
}

if columns.UserID == -1 {
return errors.New("required csv header \"user_id\" not found")
return clierrors.MissingRequiredCsvHeaderError("user_id") //nolint:wrapcheck
}

if columns.Relation == -1 {
return errors.New("required csv header \"relation\" not found")
return clierrors.MissingRequiredCsvHeaderError("relation") //nolint:wrapcheck
}

if columns.ObjectType == -1 {
return errors.New("required csv header \"object_type\" not found")
return clierrors.MissingRequiredCsvHeaderError("object_type") //nolint:wrapcheck
}

if columns.ObjectID == -1 {
return errors.New("required csv header \"object_id\" not found")
return clierrors.MissingRequiredCsvHeaderError("object_id") //nolint:wrapcheck
}

if columns.ConditionContext != -1 && columns.ConditionName == -1 {
return errors.New("missing \"condition_name\" header which is required when \"condition_context\" is present")
return errors.New("missing \"condition_name\" header which is required when \"condition_context\" is present") //nolint:goerr113
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/tuple/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestParseTuplesFileData(t *testing.T) { //nolint:funlen
{
name: "it fails to parse a csv file with missing required headers",
file: "testdata/tuples_missing_required_headers.csv",
expectedError: "failed to parse input tuples: required csv header \"object_id\" not found",
expectedError: "failed to parse input tuples: csv header missing (\"object_id\")",
},
{
name: "it fails to parse a csv file with missing condition_name header when condition_context is present",
Expand Down
5 changes: 5 additions & 0 deletions internal/clierrors/clierrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ var (
ErrStoreNotFound = errors.New("store not found")
ErrAuthorizationModelNotFound = errors.New("authorization model not found")
ErrModelInputMissing = errors.New("model input not provided")
ErrRequiredCsvHeaderMissing = errors.New("csv header missing")
)

func ValidationError(op string, details string) error {
return fmt.Errorf("%w - %s: %s", ErrValidation, op, details)
}

func MissingRequiredCsvHeaderError(headerName string) error {
return fmt.Errorf("%w (\"%s\")", ErrRequiredCsvHeaderMissing, headerName)
}

0 comments on commit d63889a

Please sign in to comment.