Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annotation positions to schema error messages #552

Merged
merged 5 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 38 additions & 34 deletions pkg/schema/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ const schemaErrorReportTemplate = `
{{- else}}
{{.FileName}}:
{{pad "|" ""}}
{{- range .AnnPositions}}
{{- range .Positions}}
{{pad "|" .Pos}} {{.Source}}
{{- if .SkipLines}}
{{pad "|" ""}} {{"..."}}
{{- end}}
{{- end}}
{{pad "|" .FilePos}} {{.Source}}
{{pad "|" ""}}
{{- end}}

Expand All @@ -59,16 +58,16 @@ func NewSchemaError(summary string, errs ...error) error {
for _, err := range errs {
if typeCheckAssertionErr, ok := err.(schemaAssertionError); ok {
failures = append(failures, assertionFailure{
Description: typeCheckAssertionErr.description,
FileName: typeCheckAssertionErr.position.GetFile(),
AnnPositions: createAnnInfo(typeCheckAssertionErr.annPositions, typeCheckAssertionErr.position),
FilePos: typeCheckAssertionErr.position.AsIntString(),
FromMemory: typeCheckAssertionErr.position.FromMemory(),
SourceName: "Data value calculated",
Source: typeCheckAssertionErr.position.GetLine(),
Expected: typeCheckAssertionErr.expected,
Found: typeCheckAssertionErr.found,
Hints: typeCheckAssertionErr.hints,
Description: typeCheckAssertionErr.description,
FileName: typeCheckAssertionErr.position.GetFile(),
Positions: createPosInfo(typeCheckAssertionErr.annPositions, typeCheckAssertionErr.position),
FilePos: typeCheckAssertionErr.position.AsIntString(),
FromMemory: typeCheckAssertionErr.position.FromMemory(),
SourceName: "Data value calculated",
Source: typeCheckAssertionErr.position.GetLine(),
Expected: typeCheckAssertionErr.expected,
Found: typeCheckAssertionErr.found,
Hints: typeCheckAssertionErr.hints,
})
} else {
miscErrorMessage += fmt.Sprintf("%s \n", err.Error())
Expand Down Expand Up @@ -133,16 +132,16 @@ type schemaError struct {
}

type assertionFailure struct {
Description string
FileName string
AnnPositions []annErr
Source string
FilePos string
FromMemory bool
SourceName string
Expected string
Found string
Hints []string
Description string
FileName string
Positions []posInfo
Source string
FilePos string
FromMemory bool
SourceName string
Expected string
Found string
Hints []string
}

type schemaAssertionError struct {
Expand All @@ -155,29 +154,34 @@ type schemaAssertionError struct {
hints []string
}

type annErr struct {
type posInfo struct {
Pos string
Source string
SkipLines bool
}
gcheadle-vmware marked this conversation as resolved.
Show resolved Hide resolved

func createAnnInfo(annPosList []*filepos.Position, nodePos *filepos.Position) []annErr {
func createPosInfo(annPosList []*filepos.Position, nodePos *filepos.Position) []posInfo {
sort.SliceStable(annPosList, func(i, j int) bool {
if !annPosList[i].IsKnown() {
return true
}
if !annPosList[j].IsKnown() {
return false
}
return annPosList[i].LineNum() < annPosList[j].LineNum()
})

var AnnPositions []annErr
for i, p := range annPosList {
//if last position in list, compare with node position
if i+1 == len(annPosList) {
skipLines := !p.IsNextTo(nodePos)
AnnPositions = append(AnnPositions, annErr{Pos: p.AsIntString(), Source: p.GetLine(), SkipLines: skipLines})
} else {
skipLines := !p.IsNextTo(annPosList[i+1])
AnnPositions = append(AnnPositions, annErr{Pos: p.AsIntString(), Source: p.GetLine(), SkipLines: skipLines})
allPositions := append(annPosList, nodePos)
var positionsInfo []posInfo
for i, p := range allPositions {
//last position in list
skipLines := false
if i < len(allPositions)-1 {
skipLines = !p.IsNextTo(allPositions[i+1])
gcheadle-vmware marked this conversation as resolved.
Show resolved Hide resolved
}
positionsInfo = append(positionsInfo, posInfo{Pos: p.AsIntString(), Source: p.GetLine(), SkipLines: skipLines})
}
return AnnPositions
return positionsInfo
}

func (e schemaError) Error() string {
Expand Down
15 changes: 10 additions & 5 deletions pkg/template/evaluation_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,15 @@ func (e *EvaluationCtx) TplStartNodeAnnotation(
}
annName := AnnotationName(annNameStr)

lineNum, err := strconv.Atoi(args.Index(2).String())
if err != nil {
//what error would this be? string should be line number... when would it not
return starlark.None, err
var position *filepos.Position
if _, ok := args.Index(2).(starlark.NoneType); ok {
position = filepos.NewUnknownPosition()
} else {
lineNum, err := strconv.Atoi(args.Index(2).String())
if err != nil {
panic(fmt.Sprintf("expected line num to be int or None, but was %v", args.Index(2).String()))
gcheadle-vmware marked this conversation as resolved.
Show resolved Hide resolved
}
position = filepos.NewPosition(lineNum)
}

annVals := args.Index(3).(starlark.Tuple)
Expand All @@ -180,7 +185,7 @@ func (e *EvaluationCtx) TplStartNodeAnnotation(
e.pendingAnnotations[nodeTag][annName] = NodeAnnotation{
Args: annVals[0].(starlark.Tuple),
Kwargs: kwargs,
Position: filepos.NewPosition(lineNum),
Position: position,
}

return starlark.None, nil
Expand Down
6 changes: 5 additions & 1 deletion pkg/template/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ func (is *InstructionSet) NewEndCtxNone() Instruction {

func (is *InstructionSet) NewStartNodeAnnotation(nodeTag NodeTag, ann Annotation) Instruction {
collectedArgs := is.CollectNodeAnnotation.WithArgs(ann.Content).AsString()
return is.StartNodeAnnotation.WithArgs(nodeTag.AsString(), `"`+string(ann.Name)+`"`, ann.Position.AsIntString(), collectedArgs)
annLineNum := "None"
if ann.Position.IsKnown() {
annLineNum = ann.Position.AsIntString()
}
gcheadle-vmware marked this conversation as resolved.
Show resolved Hide resolved
return is.StartNodeAnnotation.WithArgs(nodeTag.AsString(), `"`+string(ann.Name)+`"`, annLineNum, collectedArgs)
}

func (is *InstructionSet) NewStartNode(nodeTag NodeTag) Instruction {
Expand Down