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

o2k: reorder object members to prettify the yaml output #3340

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/openapi-2-kong/src/common.ts
Expand Up @@ -175,3 +175,27 @@ export function distinctByProperty<T>(arr: T[], propertySelector: (item: T) => a

return result;
}

/** reorders object members for nicer yaml output */
export function reorderObjectMembers(objIn: any, first: string[], last: 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;
}
30 changes: 30 additions & 0 deletions packages/openapi-2-kong/src/declarative-config/common.ts
@@ -0,0 +1,30 @@
import { reorderObjectMembers } from '../common';
import { DCPlugin, DCRoute, DCService, DCUpstream } from '../types/declarative-config';

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

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

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

export function reorderPlugins(plugins: DCPlugin[]): DCPlugin[] {
const first: string[] = ['name'];
const last: string[] = ['tags', 'config'];
const pluginsOut: DCPlugin[] = [];
plugins.forEach(plugin => {
pluginsOut.push(reorderObjectMembers(plugin, first, last));
});
return pluginsOut;
}
9 changes: 5 additions & 4 deletions packages/openapi-2-kong/src/declarative-config/services.ts
Expand Up @@ -16,6 +16,7 @@ import {
} from './plugins';
import { DCService, DCRoute } from '../types/declarative-config';
import { OpenApi3Spec, OA3Server, OA3PathItem, OA3Operation } from '../types/openapi3';
import { reorderService, reorderRoute, reorderPlugins } from './common';

export function generateServices(api: OpenApi3Spec, tags: string[]) {
const servers = getAllServers(api);
Expand Down Expand Up @@ -53,7 +54,7 @@ export function generateService(server: OA3Server, api: OpenApi3Spec, tags: stri
// 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 @@ -135,14 +136,14 @@ export function generateService(server: OA3Server, api: OpenApi3Spec, tags: stri

// 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.ts
@@ -1,6 +1,7 @@
import { getName, parseUrl, fillServerVariables } from '../common';
import { DCUpstream } from '../types/declarative-config';
import { OpenApi3Spec } from '../types/openapi3';
import { reorderUpstream } from './common';

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

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