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

feat: properties from struct #45

Merged
merged 17 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
75 changes: 75 additions & 0 deletions pkg/docs/generate.go
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
}
81 changes: 81 additions & 0 deletions pkg/docs/generate_test.go
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)
})
}
}
11 changes: 11 additions & 0 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ type Registration struct {
// different levels, whereas AWS has simply Account level.
Scope Scope

// Resource is the resource type that the lister is going to list. This is a struct that implements the Resource
// interface. This is primarily used to generate documentation by parsing the structs properties and generating
// markdown documentation.
// Note: it is a interface{} because we are going to inspect it, we do not need to actually call any methods on it.
Resource interface{}

// Lister is the lister for the resource type, it is a struct with a method called List that returns a slice
// of resources. The lister is responsible for filtering out any resources that should not be deleted because they
// are ineligible for deletion. For example, built in resources that cannot be deleted.
Expand Down Expand Up @@ -120,6 +126,11 @@ func Register(r *Registration) {
}
}

// GetRegistrations returns all registrations
func GetRegistrations() Registrations {
return registrations
}

// ClearRegistry clears the registry of all registrations
// Designed for use for unit tests, not for production code. Only use if you know what you are doing.
func ClearRegistry() {
Expand Down
13 changes: 13 additions & 0 deletions pkg/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,16 @@ func Test_RegisterResourcesWithAlternative(t *testing.T) {
assert.Len(t, deprecatedMapping, 1)
assert.Equal(t, "test2", deprecatedMapping["test"])
}

func Test_GetRegistrations(t *testing.T) {
ClearRegistry()

Register(&Registration{
Name: "test",
Scope: "test",
Lister: TestLister{},
})

regs := GetRegistrations()
assert.Len(t, regs, 1)
}
Loading
Loading