From b20cab07dd005317e2cbb7d8e18af498843eaf56 Mon Sep 17 00:00:00 2001 From: Luis Majano Date: Fri, 24 Jan 2025 20:10:59 +0100 Subject: [PATCH] struct consistencies --- .../boxlang/yaml/BoxLangRepresenter.java | 47 +++++++++++++++++++ .../java/ortus/boxlang/yaml/YamlParser.java | 2 +- .../boxlang/moduleslug/IntegrationTest.java | 42 +++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ortus/boxlang/yaml/BoxLangRepresenter.java diff --git a/src/main/java/ortus/boxlang/yaml/BoxLangRepresenter.java b/src/main/java/ortus/boxlang/yaml/BoxLangRepresenter.java new file mode 100644 index 0000000..d31ea95 --- /dev/null +++ b/src/main/java/ortus/boxlang/yaml/BoxLangRepresenter.java @@ -0,0 +1,47 @@ +/** + * [BoxLang] + * + * Copyright [2023] [Ortus Solutions, Corp] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ortus.boxlang.yaml; + +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.nodes.Node; +import org.yaml.snakeyaml.representer.Representer; + +import ortus.boxlang.runtime.scopes.Key; + +public class BoxLangRepresenter extends Representer { + + /** + * Constructor + */ + public BoxLangRepresenter( DumperOptions options ) { + super( options ); + this.representers.put( Key.class, new RepesentKey() ); + } + + private class RepesentKey extends RepresentString { + + @Override + public Node representData( Object data ) { + if ( data instanceof Key castedKey ) { + return super.representData( castedKey.getName() ); + } + return super.representData( data ); + } + } + +} diff --git a/src/main/java/ortus/boxlang/yaml/YamlParser.java b/src/main/java/ortus/boxlang/yaml/YamlParser.java index d97d916..f9af1a0 100644 --- a/src/main/java/ortus/boxlang/yaml/YamlParser.java +++ b/src/main/java/ortus/boxlang/yaml/YamlParser.java @@ -54,7 +54,7 @@ private YamlParser() { options.setDefaultFlowStyle( DumperOptions.FlowStyle.BLOCK ); options.setPrettyFlow( true ); - Representer representer = new Representer( options ); + Representer representer = new BoxLangRepresenter( options ); representer.getPropertyUtils().setSkipMissingProperties( true ); LoaderOptions loaderOptions = new LoaderOptions(); diff --git a/src/test/java/ortus/boxlang/moduleslug/IntegrationTest.java b/src/test/java/ortus/boxlang/moduleslug/IntegrationTest.java index 839aadc..8482be6 100644 --- a/src/test/java/ortus/boxlang/moduleslug/IntegrationTest.java +++ b/src/test/java/ortus/boxlang/moduleslug/IntegrationTest.java @@ -52,4 +52,46 @@ public void testCanSerializeString() { context ); assertThat( variables.get( result ) ).isEqualTo( "Hello World\n" ); } + + @DisplayName( "It can serialize a number" ) + @Test + public void testCanSerializeNumber() { + // @formatter:off + runtime.executeSource( + """ + result = yamlSerialize( 42 ) + println( result ) + """, + context ); + // @formatter:on + assertThat( variables.get( result ) ).isEqualTo( "42\n" ); + } + + @DisplayName( "It can serialize an array" ) + @Test + public void testCanSerializeArray() { + // @formatter:off + runtime.executeSource( + """ + result = yamlSerialize( [ 1, 2, 3 ] ) + println( result ) + """, + context ); + // @formatter:on + assertThat( variables.get( result ) ).isEqualTo( "- 1\n- 2\n- 3\n" ); + } + + @DisplayName( "It can serialize a struct" ) + @Test + public void testCanSerializeStruct() { + // @formatter:off + runtime.executeSource( + """ + result = yamlSerialize( { name = "Luis", age = 42 } ) + println( result ) + """, + context ); + // @formatter:on + assertThat( variables.get( result ) ).isEqualTo( "age: 42\nname: Luis\n" ); + } }