Skip to content

Commit

Permalink
Linting tfgen (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
stack72 committed Jul 1, 2021
1 parent e62dc90 commit da9b3e7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 36 deletions.
5 changes: 4 additions & 1 deletion pkg/tfbridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func Main(pkg string, version string, prov ProviderInfo, pulumiSchema []byte) {
// Ensure we do print help message when `--help` is requested.
if err == flag.ErrHelp {
flags.SetOutput(defaultOutput)
flags.Parse(os.Args[1:])
err := flags.Parse(os.Args[1:])
if err != nil {
cmdutil.ExitError(err.Error())
}
}

if *dumpInfo {
Expand Down
8 changes: 4 additions & 4 deletions pkg/tfbridge/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,11 @@ func TestProviderCheckV2(t *testing.T) {

failures := testCheckFailures(t, provider, "SecondResource")
sort.SliceStable(failures, func(i, j int) bool { return failures[i].Reason < failures[j].Reason })
assert.Equal(t, "Conflicting configuration arguments: \"conflicting_property\": conflicts with conflicting_property2. "+
"Examine values at 'SecondResource.ConflictingProperty'.", failures[0].Reason)
assert.Equal(t, "Conflicting configuration arguments: \"conflicting_property\": conflicts with "+
"conflicting_property2. Examine values at 'SecondResource.ConflictingProperty'.", failures[0].Reason)
assert.Equal(t, "", failures[0].Property)
assert.Equal(t, "Conflicting configuration arguments: \"conflicting_property2\": conflicts with conflicting_property. "+
"Examine values at 'SecondResource.ConflictingProperty2'.", failures[1].Reason)
assert.Equal(t, "Conflicting configuration arguments: \"conflicting_property2\": conflicts with "+
"conflicting_property. Examine values at 'SecondResource.ConflictingProperty2'.", failures[1].Reason)
assert.Equal(t, "", failures[1].Property)
assert.Equal(t, "Missing required argument: The argument \"array_property_value\" is required, but no "+
"definition was found.. Examine values at 'SecondResource.ArrayPropertyValues'.", failures[2].Reason)
Expand Down
2 changes: 0 additions & 2 deletions pkg/tfgen/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,6 @@ func (p *tfMarkdownParser) reformatExamples(sections [][]string) [][]string {
return result
}

var nestedSchemaPattern *regexp.Regexp = regexp.MustCompile("^[#]+ Nested Schema for [`]([^`]+)[`]")

func (p *tfMarkdownParser) parseSection(section []string) error {
// Extract the header name, since this will drive how we process the content.
if len(section) == 0 {
Expand Down
38 changes: 10 additions & 28 deletions pkg/tfgen/parse_markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (ns *topLevelSchema) allParameters() []parameter {

type nestedSchema struct {
longName string
linkId *string
linkID *string
optional []parameter
required []parameter
readonly []parameter
Expand Down Expand Up @@ -93,7 +93,8 @@ func parseTopLevelSchemaIntoDocs(
}

for _, ns := range topLevelSchema.nestedSchemata {
parseNestedSchemaIntoDocs(accumulatedDocs, &ns, warn)
nestedSchema := ns // this stops implicit memory addressing
parseNestedSchemaIntoDocs(accumulatedDocs, &nestedSchema, warn)
}
}

Expand Down Expand Up @@ -160,7 +161,7 @@ func parseNestedSchemaIntoDocs(
for _, param := range nestedSchema.allParameters() {
oldDesc, hasAlready := args.arguments[param.name]
if hasAlready && oldDesc != param.desc {
warn("Descripton conflict for param %s from %s; candidates are `%s` and `%s`",
warn("Description conflict for param %s from %s; candidates are `%s` and `%s`",
param.name,
nestedSchema.longName,
oldDesc,
Expand All @@ -170,7 +171,7 @@ func parseNestedSchemaIntoDocs(
fullParamName := fmt.Sprintf("%s.%s", nestedSchema.longName, param.name)
paramArgs, created := accumulatedDocs.getOrCreateArgumentDocs(fullParamName)
if !created && paramArgs.description != param.desc {
warn("Descripton conflict for param %s; candidates are `%s` and `%s`",
warn("Description conflict for param %s; candidates are `%s` and `%s`",
fullParamName,
paramArgs.description,
param.desc)
Expand All @@ -189,8 +190,8 @@ func parseNestedSchema(node *bf.Node, consumeNode func(node *bf.Node)) (*nestedS
return nil, nil
}

linkId := parsePreamble(node, consumeNode)
if linkId != nil {
linkID := parsePreamble(node, consumeNode)
if linkID != nil {
node = node.Next
}

Expand All @@ -211,7 +212,7 @@ func parseNestedSchema(node *bf.Node, consumeNode func(node *bf.Node)) (*nestedS

ns := &nestedSchema{
longName: string(code.Literal),
linkId: linkId,
linkID: linkID,
}

curNode := node.Next
Expand Down Expand Up @@ -365,14 +366,14 @@ func parseParameter(node *bf.Node) (*parameter, error) {
return parseParameterFromDescription(paramName, cleanDesc(paramDesc)), nil
}

var seeBelowPattern *regexp.Regexp = regexp.MustCompile("[(]see \\[below for nested schema\\][(][^)]*[)][)]")
var seeBelowPattern *regexp.Regexp = regexp.MustCompile(`[(]see \[below for nested schema\][(][^)]*[)][)]`)

func cleanDesc(desc string) string {
desc = seeBelowPattern.ReplaceAllString(desc, "")
return strings.TrimSpace(desc)
}

var descriptionTypeSectionPattern *regexp.Regexp = regexp.MustCompile("^\\s*[(]([^[)]+)[)]\\s*")
var descriptionTypeSectionPattern *regexp.Regexp = regexp.MustCompile(`^\s*[(]([^[)]+)[)]\s*`)

func parseParameterFromDescription(name string, description string) *parameter {
if descriptionTypeSectionPattern.MatchString(description) {
Expand Down Expand Up @@ -441,25 +442,6 @@ func parseTextSeq(firstNode *bf.Node, useStarsForStrongAndEmph bool) (string, er
return buffer.String(), err
}

// Useful to remember and remove nodes that were consumed
// (successfully parsed) from the AST, for example to debug which
// parts of the AST we fail to recognize.
type nodeUnlinker struct {
nodes []*bf.Node
}

func (nu *nodeUnlinker) consumeNode(node *bf.Node) {
nu.nodes = append(nu.nodes, node)
}

func (nu *nodeUnlinker) unlinkAll() {
for _, n := range nu.nodes {
if n != nil {
n.Unlink()
}
}
}

func parseDoc(text string) *bf.Node {
mdProc := bf.New(bf.WithExtensions(bf.FencedCode))
return mdProc.Parse([]byte(text))
Expand Down
3 changes: 2 additions & 1 deletion pkg/tfgen/parse_markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestParseTextSeq(t *testing.T) {
turnaround("**strong**")
turnaround("[link](http://pulumi.com)")
turnaround("plain `code` *emph* **strong** [link](http://pulumi.com)")
turnaround(`(Block List, Max: 1) The definition for a Change widget. (see [below for nested schema](#nestedblock--widget--group_definition--widget--change_definition))`)
turnaround(`(Block List, Max: 1) The definition for a Change widget. (see [below for nested schema]` +
`(#nestedblock--widget--group_definition--widget--change_definition))`)

res, err := parseTextSeq(parseNode("request_max_bytes").FirstChild, false)
if err != nil {
Expand Down

0 comments on commit da9b3e7

Please sign in to comment.