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

feat: properties from struct #45

merged 17 commits into from
Apr 21, 2024

Conversation

ekristen
Copy link
Owner

@ekristen ekristen commented Mar 28, 2024

Overview

This is laying the groundwork for allowing the aws-nuke and azure-nuke tool to update their Resource definitions to have exported variables that are then used to build the properties automatically vs having to define them manually.

There are two benefits to this, the first being that we can make the code cleaner and more simple by removing a bunch of for loops for tags, but we can also now programmatically generate documentation by inspecting the resource structs to document what properties are available.

This might make the resource structs get a little more verbose, but in the end, it'll make the use of the tool better.

Type Support

  • string
  • int
  • int64
  • map
  • slice
  • struct

Note: all pointers are dereferenced during evaluation

Features

  • any non-exported field on a struct is ignored and will not be exported as a propery
  • property:"-" will omit a field from being exported as a property
  • property:"prefix=vpc" will ensure the value of the field or the tags are prefixed with vpc

Example

type ResourceGroup struct {
  client *client
  Name string
  Region *string
  Tags map[string]string
}

types.NewPropertiesFromStruct(&ResourceGroup{
  Name: "example"
  Region: ptr.String("eastus")
  Tags: map[string]string{"key": "value"}
})

This automatically will create properties for Name, Region and Tags.

[Name: "example", Region: "eastus", tag:key: "value"]

Benchmark

goos: darwin
goarch: amd64
pkg: github.com/ekristen/libnuke/pkg/types
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkNewProperties
BenchmarkNewProperties-16                      	 2815178	       412.1 ns/op
BenchmarkNewPropertiesFromStruct_Simple
BenchmarkNewPropertiesFromStruct_Simple-16     	 1786765	       680.6 ns/op
BenchmarkNewPropertiesFromStruct_Complex
BenchmarkNewPropertiesFromStruct_Complex-16    	  679848	      1702 ns/op
PASS
ok  	github.com/ekristen/libnuke/pkg/types	4.869s

@ekristen ekristen changed the title feature: properties from struct feat: properties from struct Mar 28, 2024
Copy link

codecov bot commented Mar 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.62%. Comparing base (9c84ff2) to head (711dac1).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #45      +/-   ##
==========================================
+ Coverage   95.09%   95.62%   +0.53%     
==========================================
  Files          15       16       +1     
  Lines         897     1006     +109     
==========================================
+ Hits          853      962     +109     
  Misses         26       26              
  Partials       18       18              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ekristen ekristen merged commit 939dab9 into main Apr 21, 2024
5 checks passed
@ekristen ekristen deleted the feat-props-from-struct branch April 21, 2024 23:17
@ekristen
Copy link
Owner Author

🎉 This pull request is included in version 0.14.0 🎉

The release is available on GitHub release 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant