forked from hanneshayashi/terraform-plugin-codegen-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add Automatic Validator Value Documentation #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
d66a807
feat: automatically append `stringvalidator.OneOf` values to string a…
hanneshayashi da49981
feat: `description_validator` now correctly parses `stringvalidator.O…
hanneshayashi a0b387a
refactor: Replace regex-based parsing of `stringvalidator.OneOf` with…
hanneshayashi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package convert | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "go/ast" | ||
| "go/parser" | ||
| "go/token" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // AppendValidators parses stringvalidator.OneOf validators from the provided Validators object | ||
| // and appends the possible values to the Description. | ||
| // | ||
| // It uses go/parser to safely extract values from the schema definition, handling | ||
| // escaped quotes, commas, and other Go syntax correctly. | ||
| func (d *Description) AppendValidators(v Validators) { | ||
| if d.description == nil { | ||
| empty := "" | ||
| d.description = &empty | ||
| } | ||
|
|
||
| for _, custom := range v.custom { | ||
| if custom.SchemaDefinition == "" { | ||
| continue | ||
| } | ||
|
|
||
| // Parse the expression | ||
| expr, err := parser.ParseExpr(custom.SchemaDefinition) | ||
| if err != nil { | ||
| // If we can't parse it, we can't extract values safely. | ||
| // Just skip description augmentation. | ||
| continue | ||
| } | ||
|
|
||
| // Inspect the AST to find OneOf calls | ||
| ast.Inspect(expr, func(n ast.Node) bool { | ||
| // We look for function calls | ||
| call, ok := n.(*ast.CallExpr) | ||
| if !ok { | ||
| return true | ||
| } | ||
|
|
||
| // Check if function is stringvalidator.OneOf | ||
| // This could be a SelectorExpr (pkg.Func) | ||
| sel, ok := call.Fun.(*ast.SelectorExpr) | ||
| if !ok { | ||
| return true | ||
| } | ||
|
|
||
| // Check package name | ||
| id, ok := sel.X.(*ast.Ident) | ||
| if !ok || id.Name != "stringvalidator" { | ||
| return true | ||
| } | ||
|
|
||
| // Check function name | ||
| if sel.Sel.Name != "OneOf" { | ||
| return true | ||
| } | ||
|
|
||
| // Extract arguments | ||
| var values []string | ||
| for _, arg := range call.Args { | ||
| // We expect string literals | ||
| lit, ok := arg.(*ast.BasicLit) | ||
| if !ok || lit.Kind != token.STRING { | ||
| continue | ||
| } | ||
|
|
||
| // Unquote the string value | ||
| val, err := strconv.Unquote(lit.Value) | ||
| if err != nil { | ||
| continue | ||
| } | ||
|
|
||
| values = append(values, fmt.Sprintf("`%s`", val)) | ||
| } | ||
|
|
||
| if len(values) > 0 { | ||
| suffix := fmt.Sprintf("Possible values: %s", strings.Join(values, ", ")) | ||
|
|
||
| // Avoid appending if already present | ||
| if strings.Contains(*d.description, suffix) { | ||
| return true | ||
| } | ||
|
|
||
| if *d.description != "" { | ||
| *d.description += "\n" | ||
| } | ||
| *d.description += suffix | ||
| } | ||
|
|
||
| return true | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package convert | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| specschema "github.com/hashicorp/terraform-plugin-codegen-spec/schema" | ||
| ) | ||
|
|
||
| func TestDescription_AppendValidators(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| initialDesc *string | ||
| validators Validators | ||
| expectedDesc string | ||
| }{ | ||
| { | ||
| name: "Empty description, simple OneOf", | ||
| initialDesc: nil, | ||
| validators: NewValidators(ValidatorTypeString, specschema.CustomValidators{ | ||
| { | ||
| SchemaDefinition: `stringvalidator.OneOf("a", "b")`, | ||
| }, | ||
| }), | ||
| expectedDesc: "Possible values: `a`, `b`", | ||
| }, | ||
| { | ||
| name: "Existing description, simple OneOf", | ||
| initialDesc: stringPtr("Some description."), | ||
| validators: NewValidators(ValidatorTypeString, specschema.CustomValidators{ | ||
| { | ||
| SchemaDefinition: `stringvalidator.OneOf("foo", "bar")`, | ||
| }, | ||
| }), | ||
| expectedDesc: "Some description.\nPossible values: `foo`, `bar`", | ||
| }, | ||
| { | ||
| name: "Multiline OneOf", | ||
| initialDesc: stringPtr("Desc"), | ||
| validators: NewValidators(ValidatorTypeString, specschema.CustomValidators{ | ||
| { | ||
| SchemaDefinition: `stringvalidator.OneOf( | ||
| "val1", | ||
| "val2", | ||
| )`, | ||
| }, | ||
| }), | ||
| expectedDesc: "Desc\nPossible values: `val1`, `val2`", | ||
| }, | ||
| { | ||
| name: "Commas inside quotes", | ||
| initialDesc: stringPtr("Desc"), | ||
| validators: NewValidators(ValidatorTypeString, specschema.CustomValidators{ | ||
| { | ||
| SchemaDefinition: `stringvalidator.OneOf("a,b", "c")`, | ||
| }, | ||
| }), | ||
| expectedDesc: "Desc\nPossible values: `a,b`, `c`", | ||
| }, | ||
| { | ||
| name: "No OneOf", | ||
| initialDesc: stringPtr("Desc"), | ||
| validators: NewValidators(ValidatorTypeString, specschema.CustomValidators{ | ||
| { | ||
| SchemaDefinition: `stringvalidator.LengthAtLeast(1)`, | ||
| }, | ||
| }), | ||
| expectedDesc: "Desc", | ||
| }, | ||
| { | ||
| name: "Multiple OneOf (should append both)", | ||
| initialDesc: stringPtr("Desc"), | ||
| validators: NewValidators(ValidatorTypeString, specschema.CustomValidators{ | ||
| { | ||
| SchemaDefinition: `stringvalidator.OneOf("a", "b")`, | ||
| }, | ||
| { | ||
| // This case is artificial, usually there's only one OneOf, but good to test behavior | ||
| SchemaDefinition: `stringvalidator.OneOf("c", "d")`, | ||
| }, | ||
| }), | ||
| expectedDesc: "Desc\nPossible values: `a`, `b`\nPossible values: `c`, `d`", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| d := NewDescription(tt.initialDesc) | ||
| d.AppendValidators(tt.validators) | ||
|
|
||
| got := d.Description() | ||
| if diff := cmp.Diff(tt.expectedDesc, got); diff != "" { | ||
| t.Errorf("AppendValidators() mismatch (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func stringPtr(s string) *string { | ||
| return &s | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.