Skip to content

Commit

Permalink
struct consistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano committed Jan 24, 2025
1 parent 6ffdeb5 commit b20cab0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/main/java/ortus/boxlang/yaml/BoxLangRepresenter.java
Original file line number Diff line number Diff line change
@@ -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 );
}
}

}
2 changes: 1 addition & 1 deletion src/main/java/ortus/boxlang/yaml/YamlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/ortus/boxlang/moduleslug/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
}
}

0 comments on commit b20cab0

Please sign in to comment.