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

[schema processor] Part 3 - Modifiers and Revisions #12147 #17020

Merged
Prev Previous commit
Next Next commit
Updating naming
  • Loading branch information
MovieStoreGuy committed May 22, 2023
commit b913d185e5ed19a72e77523028bf67c0f6e98809
4 changes: 2 additions & 2 deletions processor/schemaprocessor/internal/migrate/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ type AttributeChangeSet struct {
// and be applied sequentially to ensure deterministic behavior.
type AttributeChangeSetSlice []*AttributeChangeSet
tigrannajaryan marked this conversation as resolved.
Show resolved Hide resolved

// NewAttributes allows for typed strings to be used as part
// NewAttributeChangeSet allows for typed strings to be used as part
// of the invocation that will be converted into the default string type.
func NewAttributes[Key AttributeKey, Value AttributeKey](mappings map[Key]Value) *AttributeChangeSet {
func NewAttributeChangeSet[Key AttributeKey, Value AttributeKey](mappings map[Key]Value) *AttributeChangeSet {
attr := &AttributeChangeSet{
updates: make(map[string]string, len(mappings)),
rollback: make(map[string]string, len(mappings)),
Expand Down
28 changes: 14 additions & 14 deletions processor/schemaprocessor/internal/migrate/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestNewAttributeChangeSet(t *testing.T) {
t.Run("map of strings", func(t *testing.T) {
t.Parallel()

acs := NewAttributes(map[string]string{
acs := NewAttributeChangeSet(map[string]string{
"hello": "world",
})

Expand All @@ -57,7 +57,7 @@ func TestNewAttributeChangeSet(t *testing.T) {
t.Run("typedef string values", func(t *testing.T) {
t.Parallel()

acs := NewAttributes(map[testTypeDefKey]testTypeDefValue{
acs := NewAttributeChangeSet(map[testTypeDefKey]testTypeDefValue{
"awesome": "awesome.service",
})

Expand Down Expand Up @@ -86,7 +86,7 @@ func TestAttributeChangeSetApply(t *testing.T) {
}{
{
name: "no modifications",
acs: NewAttributes(map[string]string{}),
acs: NewAttributeChangeSet(map[string]string{}),
attrs: testHelperBuildMap(func(m pcommon.Map) {
m.PutInt("test.cases", 1)
}),
Expand All @@ -96,7 +96,7 @@ func TestAttributeChangeSetApply(t *testing.T) {
},
{
name: "Apply changes",
acs: NewAttributes(map[string]string{
acs: NewAttributeChangeSet(map[string]string{
"service_version": "service.version",
}),
attrs: testHelperBuildMap(func(m pcommon.Map) {
Expand All @@ -110,7 +110,7 @@ func TestAttributeChangeSetApply(t *testing.T) {
// it forces both values to be removed from the the attributes.
{
name: "naming loop",
acs: NewAttributes(map[string]string{
acs: NewAttributeChangeSet(map[string]string{
"service.version": "service_version",
"service_version": "service.version",
}),
Expand All @@ -123,7 +123,7 @@ func TestAttributeChangeSetApply(t *testing.T) {
},
{
name: "overrides existing value",
acs: NewAttributes(map[string]string{
acs: NewAttributeChangeSet(map[string]string{
"application.name": "service.name",
}),
attrs: testHelperBuildMap(func(m pcommon.Map) {
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestAttributeChangeSetRollback(t *testing.T) {
}{
{
name: "no modifications",
acs: NewAttributes(map[string]string{}),
acs: NewAttributeChangeSet(map[string]string{}),
attrs: testHelperBuildMap(func(m pcommon.Map) {
m.PutInt("test.cases", 1)
}),
Expand All @@ -173,7 +173,7 @@ func TestAttributeChangeSetRollback(t *testing.T) {
},
{
name: "Apply changes",
acs: NewAttributes(map[string]string{
acs: NewAttributeChangeSet(map[string]string{
"service_version": "service.version",
}),
attrs: testHelperBuildMap(func(m pcommon.Map) {
Expand All @@ -185,7 +185,7 @@ func TestAttributeChangeSetRollback(t *testing.T) {
},
{
name: "naming loop",
acs: NewAttributes(map[string]string{
acs: NewAttributeChangeSet(map[string]string{
"service.version": "service_version",
"service_version": "service.version",
}),
Expand All @@ -198,7 +198,7 @@ func TestAttributeChangeSetRollback(t *testing.T) {
},
{
name: "overrides existing value",
acs: NewAttributes(map[string]string{
acs: NewAttributeChangeSet(map[string]string{
"application.name": "service.name",
}),
attrs: testHelperBuildMap(func(m pcommon.Map) {
Expand Down Expand Up @@ -248,10 +248,10 @@ func TestNewAttributeChangeSetSliceApply(t *testing.T) {
{
name: "changes defined",
changes: NewAttributeChangeSetSlice(
NewAttributes(map[string]string{
NewAttributeChangeSet(map[string]string{
"service_version": "service.version",
}),
NewAttributes(map[string]string{
NewAttributeChangeSet(map[string]string{
"service.version": "application.service.version",
}),
),
Expand Down Expand Up @@ -295,10 +295,10 @@ func TestNewAttributeChangeSetSliceApplyRollback(t *testing.T) {
{
name: "changes defined",
changes: NewAttributeChangeSetSlice(
NewAttributes(map[string]string{
NewAttributeChangeSet(map[string]string{
"service_version": "service.version",
}),
NewAttributes(map[string]string{
NewAttributeChangeSet(map[string]string{
"service.version": "application.service.version",
}),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewConditionalAttributeSet[Key AttributeKey, Value AttributeKey, Match Valu
}
return &ConditionalAttributeSet{
on: &on,
attrs: NewAttributes(mappings),
attrs: NewAttributeChangeSet(mappings),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newAttributeChangeSetSliceFromChanges(attrs ast.Attributes) *migrate.Attrib
values := make([]*migrate.AttributeChangeSet, 0, 10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason to use 10 as capacity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was just a random number I pulled from no where.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since it has no particular importance I think it can be safely deleted, values := make([]*migrate.AttributeChangeSet) should be good enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, (not sure when), but the latest version of go requires at least len and an optional cap.

Tested it here (to make sure it wasn't a linter getting it in the way)
https://go.dev/play/p/9uNpeSYw6gN

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Don't know what I was thinking. What I probably wanted to say is that you can start with an empty slice since append later will happily allocate as many as needed:

var values []*migrate.AttributeChangeSet

for _, at := range attrs.Changes {
if renamed := at.RenameAttributes; renamed != nil {
values = append(values, migrate.NewAttributes(renamed.AttributeMap))
values = append(values, migrate.NewAttributeChangeSet(renamed.AttributeMap))
}
}
return migrate.NewAttributeChangeSetSlice(values...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ func TestNewRevision(t *testing.T) {
expect: &RevisionV1{
ver: &Version{1, 0, 0},
all: migrate.NewAttributeChangeSetSlice(
migrate.NewAttributes(map[string]string{
migrate.NewAttributeChangeSet(map[string]string{
"state": "status",
}),
migrate.NewAttributes(map[string]string{
migrate.NewAttributeChangeSet(map[string]string{
"status": "state",
}),
),
resource: migrate.NewAttributeChangeSetSlice(
migrate.NewAttributes(map[string]string{
migrate.NewAttributeChangeSet(map[string]string{
"service_name": "service.name",
}),
),
Expand Down