Add Spring Boot properties to YAML converter

This commit is contained in:
Marcelo Gonçalves 2023-12-28 13:06:30 +00:00
parent 1453cc3f7b
commit 36102a4312
6 changed files with 169 additions and 0 deletions

View file

@ -1,6 +1,7 @@
import { tool as base64FileConverter } from './base64-file-converter'; import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter'; import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator'; import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as propertiesToYaml } from './properties-to-yaml';
import { tool as pdfSignatureChecker } from './pdf-signature-checker'; import { tool as pdfSignatureChecker } from './pdf-signature-checker';
import { tool as numeronymGenerator } from './numeronym-generator'; import { tool as numeronymGenerator } from './numeronym-generator';
import { tool as macAddressGenerator } from './mac-address-generator'; import { tool as macAddressGenerator } from './mac-address-generator';
@ -100,6 +101,7 @@ export const toolsByCategory: ToolCategory[] = [
listConverter, listConverter,
tomlToJson, tomlToJson,
tomlToYaml, tomlToYaml,
propertiesToYaml,
], ],
}, },
{ {

View file

@ -0,0 +1,12 @@
import { AlignJustified } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Properties to YAML',
path: '/properties-to-yaml',
description: '',
keywords: ['properties', 'yaml', 'convert', 'spring', 'spring-boot', 'parse'],
component: () => import('./properties-to-yaml.vue'),
icon: AlignJustified,
createdAt: new Date('2023-12-24'),
});

View file

@ -0,0 +1,65 @@
import { expect, test } from '@playwright/test';
test.describe('Tool - Properties to YAML', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/properties-to-yaml');
});
test('Has correct title', async ({ page }) => {
await expect(page).toHaveTitle('Properties to YAML - IT Tools');
});
test('', async ({ page }) => {
await page.getByTestId('input').fill(`
spring.mvc.async.request-timeout=-1
spring.output.ansi.enabled=NEVER
spring.config.import=optional:file:.env[.properties]
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=\${DATABASE_URI}
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.open-in-view=false
management.endpoints.web.base-path=/internal
management.endpoints.web.exposure.include[0]=health
management.endpoints.web.exposure.include[1]=info
management.endpoint.info.enabled=true
management.endpoint.health=
`.trim());
const generatedJson = await page.getByTestId('area-content').innerText();
expect(generatedJson.trim()).toEqual(`
spring:
mvc:
async:
request-timeout: "-1"
output:
ansi:
enabled: NEVER
config:
import: optional:file:.env[.properties]
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: \${DATABASE_URI}
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
database-platform: org.hibernate.dialect.MySQLDialect
open-in-view: "false"
management:
endpoints:
web:
base-path: /internal
exposure:
include:
- health
- info
endpoint:
info:
enabled: "true"
health: ""
`.trim(),
);
});
});

View file

@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import { isValidProperties } from './properties-to-yaml.service';
describe('isValidProperties', () => {
it('should return true for valid properties', () => {
const properties = 'key1=value1\nkey2=value2';
expect(isValidProperties(properties)).toBe(true);
});
it('should return false for properties with duplicate keys', () => {
const properties = 'key1=value1\nkey1=value2';
expect(isValidProperties(properties)).toBe(false);
});
it('should return false for properties with incorrect format', () => {
const properties = 'key1\nkey2=value2';
expect(isValidProperties(properties)).toBe(false);
});
});

View file

@ -0,0 +1,45 @@
import _ from 'lodash';
export { isValidProperties, parseProperties };
function isValidProperties(properties: string): boolean {
const lines = properties.split('\n');
const keys: Set<string> = new Set();
for (const line of lines) {
if (line.trim().startsWith('#') || line.trim() === '') {
continue;
}
if (!line.includes('=')) {
return false;
}
const [key, _value] = line.split('=');
if (!key) {
return false;
}
if (keys.has(key.trim())) {
return false;
}
keys.add(key.trim());
}
return true;
}
// Thanks to https://github.com/sdoxsee/env-gen
function parseProperties(properties: string): Record<string, string> {
const result = properties
.split('\n')
.filter(Boolean) // removes empty lines
.reduce((acc, line) => {
const pair = line.split('=');
_.set(acc, pair[0], pair[1]);
return acc;
}, {});
return result;
}

View file

@ -0,0 +1,26 @@
<script setup lang="ts">
import { stringify as stringifyToYaml } from 'yaml';
import { withDefaultOnError } from '../../utils/defaults';
import { isValidProperties, parseProperties } from './properties-to-yaml.service';
import type { UseValidationRule } from '@/composable/validation';
const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnError(() => stringifyToYaml(parseProperties(value)), '');
const rules: UseValidationRule<string>[] = [
{
validator: isValidProperties,
message: 'Invalid .properties given.',
},
];
</script>
<template>
<format-transformer
input-label="Your Properties"
input-placeholder="Paste your Properties here..."
output-label="YAML from your Properties"
output-language="yaml"
:input-validation-rules="rules"
:transformer="transformer"
/>
</template>