Skip to content

Commit

Permalink
Implement a hash function for string sets
Browse files Browse the repository at this point in the history
Sets of strings are pretty common. Let's not duplicate the function
necessary to create a set of strings in so many places.
  • Loading branch information
Phil Frost committed May 5, 2015
1 parent 2724153 commit 33183c0
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 64 deletions.
17 changes: 4 additions & 13 deletions builtin/providers/aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"
"time"

"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"

Expand Down Expand Up @@ -81,19 +80,15 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"load_balancers": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"vpc_zone_identifier": &schema.Schema{
Expand All @@ -102,9 +97,7 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
Computed: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"termination_policies": &schema.Schema{
Expand All @@ -113,9 +106,7 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
Computed: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"tag": autoscalingTagsSchema(),
Expand Down
17 changes: 4 additions & 13 deletions builtin/providers/aws/resource_aws_db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/awslabs/aws-sdk-go/service/iam"
"github.com/awslabs/aws-sdk-go/service/rds"

"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -132,18 +131,14 @@ func resourceAwsDbInstance() *schema.Resource {
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"security_group_names": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"final_snapshot_identifier": &schema.Schema{
Expand Down Expand Up @@ -372,9 +367,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {

// Create an empty schema.Set to hold all vpc security group ids
ids := &schema.Set{
F: func(v interface{}) int {
return hashcode.String(v.(string))
},
F: schema.HashString,
}
for _, v := range v.VPCSecurityGroups {
ids.Add(*v.VPCSecurityGroupID)
Expand All @@ -383,9 +376,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {

// Create an empty schema.Set to hold all security group names
sgn := &schema.Set{
F: func(v interface{}) int {
return hashcode.String(v.(string))
},
F: schema.HashString,
}
for _, v := range v.DBSecurityGroups {
sgn.Add(*v.DBSecurityGroupName)
Expand Down
5 changes: 1 addition & 4 deletions builtin/providers/aws/resource_aws_db_subnet_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/rds"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -37,9 +36,7 @@ func resourceAwsDbSubnetGroup() *schema.Resource {
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},
},
}
Expand Down
16 changes: 4 additions & 12 deletions builtin/providers/aws/resource_aws_elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,23 @@ func resourceAwsElb() *schema.Resource {
Optional: true,
ForceNew: true,
Computed: true,
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"instances": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"security_groups": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"source_security_group": &schema.Schema{
Expand All @@ -80,9 +74,7 @@ func resourceAwsElb() *schema.Resource {
Optional: true,
ForceNew: true,
Computed: true,
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"idle_timeout": &schema.Schema{
Expand Down
4 changes: 1 addition & 3 deletions builtin/providers/aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ func resourceAwsInstance() *schema.Resource {
Computed: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"vpc_security_group_ids": &schema.Schema{
Expand Down
4 changes: 1 addition & 3 deletions builtin/providers/aws/resource_aws_launch_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ func resourceAwsLaunchConfiguration() *schema.Resource {
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"associate_public_ip_address": &schema.Schema{
Expand Down
8 changes: 2 additions & 6 deletions builtin/providers/aws/resource_aws_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@ func resourceAwsNetworkInterface() *schema.Resource {
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"security_groups": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},

"attachment": &schema.Schema{
Expand Down
4 changes: 1 addition & 3 deletions builtin/providers/aws/resource_aws_route53_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ func resourceAwsRoute53Record() *schema.Resource {
ConflictsWith: []string{"alias"},
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
Set: schema.HashString,
},
},
}
Expand Down
9 changes: 2 additions & 7 deletions builtin/providers/aws/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/awslabs/aws-sdk-go/service/rds"
"github.com/awslabs/aws-sdk-go/service/route53"
"github.com/hashicorp/terraform/flatmap"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -38,9 +37,7 @@ func testConf() map[string]string {
}

func TestexpandIPPerms(t *testing.T) {
hash := func(v interface{}) int {
return hashcode.String(v.(string))
}
hash := schema.HashString

expanded := []interface{}{
map[string]interface{}{
Expand Down Expand Up @@ -121,9 +118,7 @@ func TestexpandIPPerms(t *testing.T) {
}

func TestExpandIPPerms_nonVPC(t *testing.T) {
hash := func(v interface{}) int {
return hashcode.String(v.(string))
}
hash := schema.HashString

expanded := []interface{}{
map[string]interface{}{
Expand Down
8 changes: 8 additions & 0 deletions helper/schema/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import (
"reflect"
"sort"
"sync"

"github.com/hashicorp/terraform/helper/hashcode"
)

// HashString hashes strings. If you want a Set of strings, this is the
// SchemaSetFunc you want.
func HashString(v interface{}) int {
return hashcode.String(v.(string))
}

// Set is a set data structure that is returned for elements of type
// TypeSet.
type Set struct {
Expand Down

0 comments on commit 33183c0

Please sign in to comment.