Skip to content

Commit 34c52fd

Browse files
committed
Tighten name search to avoid false positives
1 parent 8163f21 commit 34c52fd

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

pkg/tfgen/docs.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ func parseArgReferenceSection(subsection []string, ret *entityDocs) {
928928
unbaked = map[docsPath]struct{}{}
929929
case ast.KindListItem:
930930
// We have entered a new item
931-
if name := nodeItemName(src, node); name != "" {
931+
if name := nodeItemName(src, node.(*ast.ListItem)); name != "" {
932932
current = append(current, &namedNode{node, name, true})
933933
key := keyOf(current)
934934
setLatest()
@@ -1143,15 +1143,20 @@ func cleanDescription(path docsPath, desc string) string {
11431143
return strings.TrimRightFunc(desc, unicode.IsSpace)
11441144
}
11451145

1146-
func nodeItemName(src []byte, node ast.Node) string {
1146+
var nodeItemNameRegex = regexp.MustCompile("^`[a-zA-Z0-9_]+?`")
1147+
1148+
func nodeItemName(src []byte, node *ast.ListItem) string {
11471149
if node == nil {
11481150
return ""
11491151
}
11501152
var name string
11511153
contract.AssertNoErrorf(ast.Walk(node, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
1152-
if node.Kind() == ast.KindCodeSpan {
1154+
switch node := node.(type) {
1155+
case *ast.CodeSpan:
11531156
name = string(node.Text(src))
11541157
return ast.WalkStop, nil
1158+
case *ast.Text:
1159+
return ast.WalkStop, nil
11551160
}
11561161
return ast.WalkContinue, nil
11571162
}), "We don't return an error")

pkg/tfgen/docs_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,46 @@ type testcase struct {
106106
Expected string
107107
}
108108

109+
func TestNodeItemName(t *testing.T) {
110+
t.Parallel()
111+
tests := []struct{ listItem, expected string }{
112+
{
113+
listItem: "- <a name=\"schema\"></a>`schema` - (Optional) A JSON schema for the table.",
114+
expected: "schema",
115+
},
116+
{
117+
listItem: "* `reference_file_schema_uri` - (Optional) When creating an external table, the user can provide a reference file with",
118+
expected: "reference_file_schema_uri",
119+
},
120+
{
121+
listItem: "- foo `bar`",
122+
expected: "",
123+
},
124+
{
125+
listItem: " * `fizz` buzz",
126+
expected: "fizz",
127+
},
128+
}
129+
130+
for _, tt := range tests {
131+
tt := tt
132+
t.Run("", func(t *testing.T) {
133+
t.Parallel()
134+
src := []byte(tt.listItem)
135+
node := goldmark.New().Parser().Parse(
136+
text.NewReader(src))
137+
assert.NoError(t, ast.Walk(node, func(node ast.Node, _ bool) (ast.WalkStatus, error) {
138+
if node, ok := node.(*ast.ListItem); ok {
139+
actual := nodeItemName(src, node)
140+
assert.Equal(t, tt.expected, actual)
141+
return ast.WalkStop, nil
142+
}
143+
return ast.WalkContinue, nil
144+
}))
145+
})
146+
}
147+
}
148+
109149
func TestURLRewrite(t *testing.T) {
110150
tests := []testcase{
111151
{

pkg/tfgen/test_data/resources/bigquery_table/docs.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@
116116
"description": "The number of rows at the top of the sheet\nthat BigQuery will skip when reading the data. At least one of `range` or\n`skip_leading_rows` must be set."
117117
},
118118
"hive_partitioning_options.mode": {
119-
"description": "When set, what mode of hive partitioning to use when\nreading data. The following modes are supported.\n\nAUTO: automatically infer partition key name(s) and type(s).\n\nSTRINGS: automatically infer partition key name(s). All types are\nNot all storage formats support hive partitioning. Requesting hive\npartitioning on an unsupported format will lead to an error.\nCurrently supported formats are: JSON, CSV, ORC, Avro and Parquet."
120-
},
121-
"hive_partitioning_options.mode.CUSTOM": {
122-
"description": "CUSTOM: when set to `CUSTOM`, you must encode the partition key schema within the `source_uri_prefix` by setting `source_uri_prefix` to `gs://bucket/path_to_table/{key1:TYPE1}/{key2:TYPE2}/{key3:TYPE3}`."
119+
"description": "When set, what mode of hive partitioning to use when\nreading data. The following modes are supported.\n\nAUTO: automatically infer partition key name(s) and type(s).\n\nSTRINGS: automatically infer partition key name(s). All types are\nNot all storage formats support hive partitioning. Requesting hive\npartitioning on an unsupported format will lead to an error.\nCurrently supported formats are: JSON, CSV, ORC, Avro and Parquet.\n\nCUSTOM: when set to `CUSTOM`, you must encode the partition key schema within the `source_uri_prefix` by setting `source_uri_prefix` to `gs://bucket/path_to_table/{key1:TYPE1}/{key2:TYPE2}/{key3:TYPE3}`."
123120
},
124121
"json_options.encoding": {
125122
"description": "The character encoding of the data. The supported values are UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, and UTF-32LE. The default value is UTF-8."

0 commit comments

Comments
 (0)