-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from ekristen/feat-props-from-struct
feat: properties from struct
- Loading branch information
Showing
6 changed files
with
514 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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,75 @@ | ||
package docs | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
func GeneratePropertiesMap(data interface{}) map[string]string { | ||
properties := map[string]string{} | ||
|
||
v := reflect.ValueOf(data) | ||
if v.Kind() == reflect.Ptr { | ||
v = v.Elem() | ||
} | ||
t := v.Type() | ||
|
||
for i := 0; i < t.NumField(); i++ { | ||
field := t.Field(i) | ||
|
||
if !field.IsExported() { | ||
continue | ||
} | ||
|
||
propertyTag := field.Tag.Get("property") | ||
options := strings.Split(propertyTag, ",") | ||
name := field.Name | ||
prefix := "" | ||
|
||
if options[0] == "-" { | ||
continue | ||
} | ||
|
||
for _, option := range options { | ||
parts := strings.Split(option, "=") | ||
if len(parts) != 2 { | ||
continue | ||
} | ||
switch parts[0] { | ||
case "name": | ||
name = parts[1] | ||
case "prefix": | ||
prefix = parts[1] | ||
} | ||
} | ||
|
||
if prefix != "" && name != "Tags" { | ||
name = fmt.Sprintf("%s:%s", prefix, name) | ||
} | ||
|
||
descriptionTag := field.Tag.Get("description") | ||
|
||
if name == "Tags" { | ||
originalName := name | ||
name = "tag:<key>:" | ||
tagPrefix := "tag:" | ||
if prefix != "" { | ||
tagPrefix = fmt.Sprintf("tag:%s:", prefix) | ||
} | ||
|
||
descriptionTag = fmt.Sprintf( | ||
"This resource has tags with property `%s`. These are key/value pairs that are\n\t"+ | ||
"added as their own property with the prefix of `%s` (e.g. [%sexample: \"value\"]) ", | ||
originalName, tagPrefix, tagPrefix) | ||
|
||
if prefix != "" { | ||
name = fmt.Sprintf("tag:%s:<key>:", prefix) | ||
} | ||
} | ||
|
||
properties[name] = descriptionTag | ||
} | ||
|
||
return properties | ||
} |
This file contains 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,81 @@ | ||
package docs | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenerateProperties(t *testing.T) { | ||
type TestResource1 struct { | ||
Name string `description:"The name of the resource"` | ||
Region *string `description:"The region in which the resource resides"` | ||
VpcID string `description:"The VPC ID of the resource" property:"prefix=vpc"` | ||
Tags map[string]string `description:"The tags associated with the resource"` | ||
} | ||
|
||
type TestResource2 struct { | ||
Name string `description:"The name of the resource"` | ||
Region *string `description:"The region in which the resource resides"` | ||
Tags map[string]string `description:"The tags associated with the resource" property:"prefix=ee"` | ||
} | ||
|
||
type TestResource3 struct { | ||
Name string `description:"The name of the resource"` | ||
Ignore string `property:"-"` | ||
Example string `description:"A property rename" property:"name=Delta"` | ||
skipped string //nolint:unused | ||
} | ||
|
||
cases := []struct { | ||
name string | ||
in interface{} | ||
want map[string]string | ||
}{ | ||
{ | ||
name: "TestResource1", | ||
in: TestResource1{}, | ||
want: map[string]string{ | ||
"Name": "The name of the resource", | ||
"Region": "The region in which the resource resides", | ||
"vpc:VpcID": "The VPC ID of the resource", | ||
"tag:<key>:": "This resource has tags with property `Tags`. These are key/value pairs that are\n\t" + | ||
"added as their own property with the prefix of `tag:` (e.g. [tag:example: \"value\"]) ", | ||
}, | ||
}, | ||
{ | ||
name: "TestResource2", | ||
in: TestResource2{}, | ||
want: map[string]string{ | ||
"Name": "The name of the resource", | ||
"Region": "The region in which the resource resides", | ||
"tag:ee:<key>:": "This resource has tags with property `Tags`. These are key/value pairs that are\n\t" + | ||
"added as their own property with the prefix of `tag:ee:" + | ||
"` (e.g. [tag:ee:example: \"value\"]) ", | ||
}, | ||
}, | ||
{ | ||
name: "TestResource3", | ||
in: TestResource3{}, | ||
want: map[string]string{ | ||
"Name": "The name of the resource", | ||
"Delta": "A property rename", | ||
}, | ||
}, | ||
{ | ||
name: "PointerTestResource3", | ||
in: &TestResource3{}, | ||
want: map[string]string{ | ||
"Name": "The name of the resource", | ||
"Delta": "A property rename", | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
have := GeneratePropertiesMap(c.in) | ||
assert.Equal(t, c.want, have) | ||
}) | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.