Skip to content

Commit

Permalink
fix(plugins): properly compare against foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Jul 6, 2023
1 parent 2e1aa14 commit 2fee027
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
7 changes: 5 additions & 2 deletions plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (ts *Plugger) SetSelectors(selectors []string) error {
ts.selectors = compiledSelectors
ts.pluginOwners = nil // clear previous JSONpointer search results
ts.pluginMain = nil
logbasics.Debug("successfully compiled JSONpaths")
logbasics.Debug("successfully compiled JSONpaths", selectors)
return nil
}

Expand Down Expand Up @@ -220,6 +220,9 @@ func (ts *Plugger) addPluginToOwners(newPlugin *yaml.Node, overwrite bool) error
nameNode := yamlbasics.GetFieldValue(newPlugin, "name")
if nameNode != nil && nameNode.Kind == yaml.ScalarNode {
pluginName = nameNode.Value
logbasics.Info("adding plugin", "name", pluginName)
} else {
logbasics.Info("plugin has no name", "plugin", newPlugin)
}
}

Expand Down Expand Up @@ -272,7 +275,7 @@ func (ts *Plugger) addPluginToOwners(newPlugin *yaml.Node, overwrite bool) error

// findPlugin the plugin in the owner
var findPlugin yamlbasics.YamlArrayIterator
if owner == ts.pluginMain {
if ownerPluginArray == ts.pluginMain {
findPlugin = yamlbasics.Search(ownerPluginArray, mainSearcher)
} else {
findPlugin = yamlbasics.Search(ownerPluginArray, genericSearcher)
Expand Down
109 changes: 109 additions & 0 deletions plugins/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,115 @@ var _ = Describe("plugins", func() {
}`))
})

It("adds plugin with non-matching foreign keys in the main array", func() {
dataInput := []byte(`
{ "services": [
{ "name": "service1" }
],
"plugins": [
{
"name": "plugin1",
"service": "service1",
"route": "route1",
"consumer": "consumer1",
"consumer_group": "consumer_group1"
}
]
}`)

plugger := plugins.Plugger{}
plugger.SetData(filebasics.MustDeserialize(&dataInput))
plugger.SetSelectors([]string{
"$",
})
err := plugger.AddPlugin(map[string]interface{}{
"name": "plugin1",
"service": "service2", // this one is different
"route": "route1",
"consumer": "consumer1",
"consumer_group": "consumer_group1",
}, true)
Expect(err).ToNot(HaveOccurred())

result := *(filebasics.MustSerialize(plugger.GetData(), filebasics.OutputFormatJSON))
Expect(result).To(MatchJSON(`
{
"services": [
{
"name": "service1"
}
],
"plugins": [
{
"name": "plugin1",
"service": "service1",
"route": "route1",
"consumer": "consumer1",
"consumer_group": "consumer_group1"
},
{
"name": "plugin1",
"service": "service2",
"route": "route1",
"consumer": "consumer1",
"consumer_group": "consumer_group1"
}
]
}`))
})

It("overwrites plugin with matching foreign keys in the main array", func() {
dataInput := []byte(`
{ "services": [
{ "name": "service1" }
],
"plugins": [
{
"name": "plugin1",
"service": "service1",
"route": "route1",
"consumer": "consumer1",
"consumer_group": "consumer_group1"
}
]
}`)

plugger := plugins.Plugger{}
plugger.SetData(filebasics.MustDeserialize(&dataInput))
plugger.SetSelectors([]string{
"$",
})
err := plugger.AddPlugin(map[string]interface{}{
"name": "plugin1",
"service": "service1",
"route": "route1",
"consumer": "consumer1",
"consumer_group": "consumer_group1",
"plugin": "was overwritten",
}, true)
Expect(err).ToNot(HaveOccurred())

result := *(filebasics.MustSerialize(plugger.GetData(), filebasics.OutputFormatJSON))
Expect(result).To(MatchJSON(`
{
"services": [
{
"name": "service1"
}
],
"plugins": [
{
"name": "plugin1",
"service": "service1",
"route": "route1",
"consumer": "consumer1",
"consumer_group": "consumer_group1",
"plugin": "was overwritten"
}
]
}`))
})

It("fails to add plugin to nested array with service foreign key", func() {
dataInput := []byte(`
{ "services": [
Expand Down

0 comments on commit 2fee027

Please sign in to comment.