Skip to content

Commit

Permalink
o2k: reorder object members to prettify the yaml output
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Apr 30, 2021
1 parent bdb4b4e commit ce4e209
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
24 changes: 24 additions & 0 deletions packages/openapi-2-kong/src/common.js
Expand Up @@ -170,3 +170,27 @@ export function distinctByProperty<T>(arr: Array<T>, propertySelector: (item: T)
}
return result;
}

// reorders object members for nicer yaml output
export function reorderObjectMembers(objIn: any, first: Array<string>, last: Array<string>): any {
const objOut = {};

first.forEach(propName => {
if (typeof objIn[propName] !== 'undefined') {
objOut[propName] = objIn[propName];
}
});

for (const [propName, value] of Object.entries(objIn)) {
if (typeof objOut[propName] === 'undefined' && !last.includes(propName)) {
objOut[propName] = value;
}
}

last.forEach(propName => {
if (typeof objIn[propName] !== 'undefined') {
objOut[propName] = objIn[propName];
}
});
return objOut;
}
31 changes: 31 additions & 0 deletions packages/openapi-2-kong/src/declarative-config/common.js
@@ -0,0 +1,31 @@
// @flow

import { reorderObjectMembers } from '../common';

export function reorderService(service: DCService): DCService {
const first: Array<string> = ['name', 'protocol', 'host', 'port', 'path'];
const last: Array<string> = ['tags', 'plugins', 'routes'];
return reorderObjectMembers(service, first, last);
}

export function reorderUpstream(upstream: DCUpstream): DCUpstream {
const first: Array<string> = ['name'];
const last: Array<string> = ['tags', 'targets'];
return reorderObjectMembers(upstream, first, last);
}

export function reorderRoute(route: DCRoute): DCRoute {
const first: Array<string> = ['name', 'paths', 'methods'];
const last: Array<string> = ['tags', 'plugins'];
return reorderObjectMembers(route, first, last);
}

export function reorderPlugins(plugins: Array<DCPlugin>): Array<DCPlugin> {
const first: Array<string> = ['name'];
const last: Array<string> = ['tags', 'config'];
const pluginsOut: Array<DCPlugin> = [];
plugins.forEach(plugin => {
pluginsOut.push(reorderObjectMembers(plugin, first, last));
});
return pluginsOut;
}
10 changes: 6 additions & 4 deletions packages/openapi-2-kong/src/declarative-config/services.js
Expand Up @@ -10,6 +10,8 @@ import {
parseUrl,
} from '../common';

import { reorderService, reorderRoute, reorderPlugins } from './common';

import { generateSecurityPlugins } from './security-plugins';
import {
generateOperationPlugins,
Expand Down Expand Up @@ -58,7 +60,7 @@ export function generateService(
host: name, // not a hostname, but the Upstream name
port: Number(parsedUrl.port || '80'),
path: parsedUrl.pathname,
plugins: globalPlugins.plugins,
plugins: reorderPlugins(globalPlugins.plugins),
routes: [],
tags,
};
Expand Down Expand Up @@ -139,14 +141,14 @@ export function generateService(

// Add plugins if there are any
if (plugins.length) {
route.plugins = plugins;
route.plugins = reorderPlugins(plugins);
}

service.routes.push(route);
service.routes.push(reorderRoute(route));
}
}

return service;
return reorderService(service);
}

export function generateRouteName(
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-2-kong/src/declarative-config/upstreams.js
@@ -1,6 +1,7 @@
// @flow

import { getName, parseUrl, fillServerVariables } from '../common';
import { reorderUpstream } from './common';

export function generateUpstreams(api: OpenApi3Spec, tags: Array<string>) {
const servers = api.servers || [];
Expand Down Expand Up @@ -37,5 +38,5 @@ export function generateUpstreams(api: OpenApi3Spec, tags: Array<string>) {
}
}

return [upstream];
return [reorderUpstream(upstream)];
}

0 comments on commit ce4e209

Please sign in to comment.