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 3 commits
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
51 changes: 40 additions & 11 deletions pkg/cmd/template/schema_author_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ Invalid schema
unknown @schema/type annotation keyword argument
schema.yml:
|
3 | #@schema/type unknown_kwarg=False
4 | foo: 0
|

= found: unknown_kwarg
= found: unknown_kwarg (by schema.yml:3)
= expected: A valid kwarg
= hint: Supported kwargs are 'any'
`
Expand All @@ -222,10 +223,11 @@ Invalid schema
unknown @schema/type annotation keyword argument
schema.yml:
|
3 | #@schema/type any=1
4 | foo: 0
|

= found: starlark.Int
= found: starlark.Int (by schema.yml:3)
= expected: starlark.Bool
= hint: Supported kwargs are 'any'
`
Expand All @@ -250,10 +252,11 @@ Invalid schema
expected @schema/type annotation to have keyword argument and value
schema.yml:
|
3 | #@schema/type
4 | foo: 0
|

= found: missing keyword argument and value
= found: missing keyword argument and value (by schema.yml:3)
= expected: valid keyword argument and value
= hint: Supported key-value pairs are 'any=True', 'any=False'
`
Expand All @@ -269,11 +272,27 @@ schema.yml:
#@schema/type any
foo: 0
`
expectedErr2 := `
Invalid schema
==============

expected @schema/type annotation to have keyword argument and value
schema.yml:
|
3 | #@schema/type any
4 | foo: 0
|

= found: missing keyword argument and value (by schema.yml:3)
= expected: valid keyword argument and value
= hint: Supported key-value pairs are 'any=True', 'any=False'
`

filesToProcess = files.NewSortedFiles([]*files.File{
files.MustNewFileFromSource(files.NewBytesSource("schema.yml", []byte(schemaYAML2))),
})

assertFails(t, filesToProcess, expectedErr, opts)
assertFails(t, filesToProcess, expectedErr2, opts)
})
t.Run("when schema/type and schema/nullable annotate a map", func(t *testing.T) {
schemaYAML := `#@data/values-schema
Expand All @@ -290,6 +309,8 @@ Invalid schema
@schema/nullable, and @schema/type any=True are mutually exclusive
schema.yml:
|
3 | #@schema/type any=True
4 | #@schema/nullable
5 | foo: 0
|
gcheadle-vmware marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -319,10 +340,11 @@ Invalid schema
syntax error in @schema/default annotation
schema.yml:
|
3 | #@schema/default
4 | foo: 0
|

= found: missing value (in @schema/default above this item)
= found: missing value in @schema/default (by schema.yml:3)
= expected: integer (by schema.yml:4)
`

Expand All @@ -345,10 +367,11 @@ Invalid schema
syntax error in @schema/default annotation
schema.yml:
|
3 | #@schema/default 1, 2
4 | foo: 0
|

= found: 2 values (in @schema/default above this item)
= found: 2 values in @schema/default (by schema.yml:3)
= expected: integer (by schema.yml:4)
`

Expand All @@ -371,10 +394,11 @@ Invalid schema
syntax error in @schema/default annotation
schema.yml:
|
3 | #@schema/default any=True
4 | foo: 0
|

= found: (keyword argument in @schema/default above this item)
= found: keyword argument in @schema/default (by schema.yml:3)
= expected: integer (by schema.yml:4)
= hint: this annotation only accepts one argument: the default value.
= hint: value must be in Starlark format, e.g.: {'key': 'value'}, True.
Expand All @@ -401,6 +425,7 @@ Invalid schema - @schema/default not supported on array item

schema.yml:
|
4 | #@schema/default "baz"
5 | - bar
|

Expand Down Expand Up @@ -489,10 +514,11 @@ Invalid schema
syntax error in @schema/desc annotation
schema.yml:
|
3 | #@schema/desc
4 | key: val
|

= found: missing value (in @schema/desc above this item)
= found: missing value in @schema/desc (by schema.yml:3)
= expected: string
`

Expand All @@ -515,10 +541,11 @@ Invalid schema
syntax error in @schema/desc annotation
schema.yml:
|
3 | #@schema/desc "two", "strings"
4 | key: val
|

= found: 2 values (in @schema/desc above this item)
= found: 2 values in @schema/desc (by schema.yml:3)
= expected: string
`

Expand All @@ -541,10 +568,11 @@ Invalid schema
syntax error in @schema/desc annotation
schema.yml:
|
3 | #@schema/desc 1
4 | key: val
|

= found: Non-string value (in @schema/desc above this item)
= found: Non-string value in @schema/desc (by schema.yml:3)
= expected: string
`

Expand All @@ -567,10 +595,11 @@ Invalid schema
syntax error in @schema/desc annotation
schema.yml:
|
3 | #@schema/desc key=True
4 | key: val
|

= found: keyword argument (in @schema/desc above this item)
= found: keyword argument in @schema/desc (by schema.yml:3)
= expected: string
= hint: this annotation only accepts one argument: a string.
`
Expand Down
13 changes: 13 additions & 0 deletions pkg/filepos/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,16 @@ func (p *Position) DeepCopyWithLineOffset(offset int) *Position {
*newPos.lineNum += offset
return newPos
}

// IsNextTo compares the location of one position with another.
func (p *Position) IsNextTo(otherPostion *Position) bool {
gcheadle-vmware marked this conversation as resolved.
Show resolved Hide resolved
if p.IsKnown() && otherPostion.IsKnown() {
if p.GetFile() == otherPostion.GetFile() {
diff := p.LineNum() - otherPostion.LineNum()
if -1 <= diff && 1 >= diff {
return true
}
}
}
return false
}
Loading