Skip to content

Commit

Permalink
Merge pull request #2007 from josephschorr/warning-source-code
Browse files Browse the repository at this point in the history
Add an extra `source_code` field to developer warnings
  • Loading branch information
vroldanbet authored Jul 31, 2024
2 parents 567d33f + 4a670ed commit d52dfd0
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 184 deletions.
6 changes: 6 additions & 0 deletions pkg/development/warningdefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ var lintRelationReferencesParentType = relationCheck{
return warningForMetadata(
"relation-name-references-parent",
fmt.Sprintf("Permission %q references parent type %q in its name; it is recommended to drop the suffix", relation.Name, parentDef.Name),
relation.Name,
relation,
), nil
}

return warningForMetadata(
"relation-name-references-parent",
fmt.Sprintf("Relation %q references parent type %q in its name; it is recommended to drop the suffix", relation.Name, parentDef.Name),
relation.Name,
relation,
), nil
}
Expand All @@ -53,6 +55,7 @@ var lintPermissionReferencingItself = computedUsersetCheck{
return warningForPosition(
"permission-references-itself",
fmt.Sprintf("Permission %q references itself, which will cause an error to be raised due to infinite recursion", permName),
permName,
sourcePosition,
), nil
}
Expand Down Expand Up @@ -105,6 +108,7 @@ var lintArrowReferencingUnreachable = ttuCheck{
ttu.ComputedUserset.Relation,
ttu.Tupleset.Relation,
),
fmt.Sprintf("%s->%s", ttu.Tupleset.Relation, ttu.ComputedUserset.Relation),
sourcePosition,
), nil
}
Expand Down Expand Up @@ -146,6 +150,7 @@ var lintArrowOverSubRelation = ttuCheck{
subjectType.Relation,
subjectType.Namespace,
),
fmt.Sprintf("%s->%s", ttu.Tupleset.Relation, ttu.ComputedUserset.Relation),
sourcePosition,
), nil
}
Expand Down Expand Up @@ -199,6 +204,7 @@ var lintArrowReferencingRelation = ttuCheck{
targetRelation.Name,
subjectType.Namespace,
),
fmt.Sprintf("%s->%s", ttu.Tupleset.Relation, ttu.ComputedUserset.Relation),
sourcePosition,
), nil
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/development/warnings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@ var allChecks = checks{
},
}

func warningForMetadata(warningName string, message string, metadata namespace.WithSourcePosition) *devinterface.DeveloperWarning {
return warningForPosition(warningName, message, metadata.GetSourcePosition())
func warningForMetadata(warningName string, message string, sourceCode string, metadata namespace.WithSourcePosition) *devinterface.DeveloperWarning {
return warningForPosition(warningName, message, sourceCode, metadata.GetSourcePosition())
}

func warningForPosition(warningName string, message string, sourcePosition *corev1.SourcePosition) *devinterface.DeveloperWarning {
func warningForPosition(warningName string, message string, sourceCode string, sourcePosition *corev1.SourcePosition) *devinterface.DeveloperWarning {
if sourcePosition == nil {
return &devinterface.DeveloperWarning{
Message: message,
Message: message,
SourceCode: sourceCode,
}
}

lineNumber := sourcePosition.ZeroIndexedLineNumber + 1
columnNumber := sourcePosition.ZeroIndexedColumnPosition + 1

return &devinterface.DeveloperWarning{
Message: message + " (" + warningName + ")",
Line: uint32(lineNumber),
Column: uint32(columnNumber),
Message: message + " (" + warningName + ")",
Line: uint32(lineNumber),
Column: uint32(columnNumber),
SourceCode: sourceCode,
}
}

Expand Down
49 changes: 28 additions & 21 deletions pkg/development/warnings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func TestWarnings(t *testing.T) {
permission view = view
}`,
expectedWarning: &developerv1.DeveloperWarning{
Message: "Permission \"view\" references itself, which will cause an error to be raised due to infinite recursion (permission-references-itself)",
Line: 2,
Column: 23,
Message: "Permission \"view\" references itself, which will cause an error to be raised due to infinite recursion (permission-references-itself)",
Line: 2,
Column: 23,
SourceCode: "view",
},
},
{
Expand All @@ -49,9 +50,10 @@ func TestWarnings(t *testing.T) {
permission view = viewer + (editor & view)
}`,
expectedWarning: &developerv1.DeveloperWarning{
Message: "Permission \"view\" references itself, which will cause an error to be raised due to infinite recursion (permission-references-itself)",
Line: 4,
Column: 42,
Message: "Permission \"view\" references itself, which will cause an error to be raised due to infinite recursion (permission-references-itself)",
Line: 4,
Column: 42,
SourceCode: "view",
},
},
{
Expand All @@ -68,9 +70,10 @@ func TestWarnings(t *testing.T) {
}
`,
expectedWarning: &developerv1.DeveloperWarning{
Message: "Arrow `group->member` under permission \"view\" references relation \"member\" on definition \"group\"; it is recommended to point to a permission (arrow-references-relation)",
Line: 9,
Column: 23,
Message: "Arrow `group->member` under permission \"view\" references relation \"member\" on definition \"group\"; it is recommended to point to a permission (arrow-references-relation)",
Line: 9,
Column: 23,
SourceCode: "group->member",
},
},
{
Expand All @@ -86,9 +89,10 @@ func TestWarnings(t *testing.T) {
}
`,
expectedWarning: &developerv1.DeveloperWarning{
Message: "Arrow `group->member` under permission \"view\" references relation/permission \"member\" that does not exist on any subject types of relation \"group\" (arrow-references-unreachable-relation)",
Line: 8,
Column: 23,
Message: "Arrow `group->member` under permission \"view\" references relation/permission \"member\" that does not exist on any subject types of relation \"group\" (arrow-references-unreachable-relation)",
Line: 8,
Column: 23,
SourceCode: "group->member",
},
},
{
Expand All @@ -106,9 +110,10 @@ func TestWarnings(t *testing.T) {
}
`,
expectedWarning: &developerv1.DeveloperWarning{
Message: "Arrow `parent_group->member` under permission \"view\" references relation \"parent_group\" that has relation \"member\" on subject \"group\": *the subject relation will be ignored for the arrow* (arrow-walks-subject-relation)",
Line: 10,
Column: 23,
Message: "Arrow `parent_group->member` under permission \"view\" references relation \"parent_group\" that has relation \"member\" on subject \"group\": *the subject relation will be ignored for the arrow* (arrow-walks-subject-relation)",
Line: 10,
Column: 23,
SourceCode: "parent_group->member",
},
},
{
Expand All @@ -120,9 +125,10 @@ func TestWarnings(t *testing.T) {
permission view_document = viewer
}`,
expectedWarning: &developerv1.DeveloperWarning{
Message: "Permission \"view_document\" references parent type \"document\" in its name; it is recommended to drop the suffix (relation-name-references-parent)",
Line: 5,
Column: 5,
Message: "Permission \"view_document\" references parent type \"document\" in its name; it is recommended to drop the suffix (relation-name-references-parent)",
Line: 5,
Column: 5,
SourceCode: "view_document",
},
},
{
Expand Down Expand Up @@ -169,9 +175,10 @@ func TestWarnings(t *testing.T) {
permission view = view
}`,
expectedWarning: &developerv1.DeveloperWarning{
Message: "Permission \"view\" references itself, which will cause an error to be raised due to infinite recursion (permission-references-itself)",
Line: 3,
Column: 23,
Message: "Permission \"view\" references itself, which will cause an error to be raised due to infinite recursion (permission-references-itself)",
Line: 3,
Column: 23,
SourceCode: "view",
},
},
}
Expand Down
Loading

0 comments on commit d52dfd0

Please sign in to comment.