@@ -530,10 +530,14 @@ func insertPlugin(list *[]*map[string]interface{}, newPlugin *map[string]interfa
530
530
// getForeignKeyPlugins checks the pluginList for plugins that also have a foreign key
531
531
// for a consumer, and moves them to the docPlugins array. Returns update docPlugins and pluginList.
532
532
func getForeignKeyPlugins (
533
- docPlugins * []* map [string ]interface {},
534
- pluginList * []* map [string ]interface {},
535
- foreignKey string , foreignValue string ,
536
- ) (* []* map [string ]interface {}, * []* map [string ]interface {}) {
533
+ docPlugins * []* map [string ]interface {}, // the current list of doc-level plugins (may be nil)
534
+ pluginList * []* map [string ]interface {}, // the list of entity-level plugins to check for foreign keys (may be nil)
535
+ foreignKey string , // the owner entity type: eg. "service", or "route"
536
+ foreignValue string , // the owner entity name/id: the value (service/route name)
537
+ ) (
538
+ * []* map [string ]interface {}, // updated slice of document level plugins
539
+ * []* map [string ]interface {}, // updated slice of entity level plugins
540
+ ) {
537
541
var genericPlugins []* map [string ]interface {}
538
542
if docPlugins == nil {
539
543
genericPlugins = make ([]* map [string ]interface {}, 0 )
@@ -1119,13 +1123,38 @@ func Convert(content []byte, opts O2kOptions) (map[string]interface{}, error) {
1119
1123
result ["services" ] = services
1120
1124
result ["upstreams" ] = upstreams
1121
1125
if len (* foreignKeyPlugins ) > 0 {
1126
+
1127
+ // getSortKey returns a string that can be used to sort the plugins by name, service, route, and consumer (all
1128
+ // the foreign keys that are possible).
1129
+ getSortKey := func (p * map [string ]interface {}) string {
1130
+ plugin := * p
1131
+ sep := string ([]byte {0 })
1132
+ key := plugin ["name" ].(string ) + sep
1133
+
1134
+ if plugin ["service" ] != nil {
1135
+ key = key + plugin ["service" ].(string ) + sep
1136
+ } else {
1137
+ key = key + sep
1138
+ }
1139
+
1140
+ if plugin ["route" ] != nil {
1141
+ key = key + plugin ["route" ].(string ) + sep
1142
+ } else {
1143
+ key = key + sep
1144
+ }
1145
+
1146
+ if plugin ["consumer" ] != nil {
1147
+ key = key + plugin ["consumer" ].(string ) + sep
1148
+ } else {
1149
+ key = key + sep
1150
+ }
1151
+
1152
+ return key
1153
+ }
1154
+
1122
1155
sort .Slice (* foreignKeyPlugins ,
1123
1156
func (i , j int ) bool {
1124
- p1 := * (* foreignKeyPlugins )[i ]
1125
- p2 := * (* foreignKeyPlugins )[j ]
1126
- k1 := p1 ["name" ].(string ) + p1 ["id" ].(string )
1127
- k2 := p2 ["name" ].(string ) + p2 ["id" ].(string )
1128
- return k1 < k2
1157
+ return getSortKey ((* foreignKeyPlugins )[i ]) < getSortKey ((* foreignKeyPlugins )[j ])
1129
1158
})
1130
1159
result ["plugins" ] = foreignKeyPlugins
1131
1160
}
0 commit comments