Skip to content

Commit

Permalink
Fix #509 (add IonValueMapper.builder() overloads) (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Aug 15, 2024
1 parent 294051d commit 2fb7e1d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.amazon.ion.IonSystem;
import com.amazon.ion.IonValue;
import com.amazon.ion.system.IonSystemBuilder;

/**
* Supports serializing Ion to POJO and back using the Jackson Ion framework.
Expand Down Expand Up @@ -52,11 +53,27 @@ public IonValueMapper(IonSystem ionSystem) {
this(ionSystem, null);
}

// @since 2.18: needed for `copy()`
/**
* Needed for `copy()`
*
* @since 2.18
*/
protected IonValueMapper(IonValueMapper src) {
super(src);
}

/**
* Needed for some builders
*
* @since 2.18
*/
protected IonValueMapper(IonFactory f, PropertyNamingStrategy strategy) {
super(f);
this.registerModule(new IonValueModule());
this.registerModule(new EnumAsIonSymbolModule());
this.setPropertyNamingStrategy(strategy);
}

/**
* Constructor that provides an override on the default Constructor for the PropertyNamingStrategy.
*
Expand All @@ -66,19 +83,61 @@ protected IonValueMapper(IonValueMapper src) {
* {@link PropertyNamingStrategy}
*/
public IonValueMapper(IonSystem ionSystem, PropertyNamingStrategy strategy) {
super(new IonFactory(null, ionSystem));
this.registerModule(new IonValueModule());
this.registerModule(new EnumAsIonSymbolModule());
this.setPropertyNamingStrategy(strategy);
this(new IonFactory(null, ionSystem), strategy);
}

/*
/**********************************************************************
/* Life-cycle, builders
/*
/* NOTE: must "override" (mask) all static methods from parent class
/* (most of which just call basic `builder()` or `builder(IonSystem)`
/**********************************************************************
*/

// TODO: add overrides
public static Builder builder() {
return builder(IonSystemBuilder.standard().build());
}

public static Builder builder(IonSystem ionSystem) {
return builder(ionSystem, null);
}

/**
* Canonical {@code builder()} method that most other methods
* ultimately call.
*/
public static Builder builder(IonSystem ionSystem, PropertyNamingStrategy strategy) {
return new Builder(new IonValueMapper(ionSystem, strategy));
}

public static Builder builderForBinaryWriters() {
return builderForBinaryWriters(IonSystemBuilder.standard().build());
}

public static Builder builderForBinaryWriters(IonSystem ionSystem) {
return builder(IonFactory.builderForBinaryWriters()
.ionSystem(ionSystem)
.build());
}

public static Builder builderForTextualWriters() {
return builderForTextualWriters(IonSystemBuilder.standard().build());
}

public static Builder builderForTextualWriters(IonSystem ionSystem) {
return builder(IonFactory.builderForTextualWriters()
.ionSystem(ionSystem)
.build());
}

public static Builder builder(IonFactory streamFactory) {
return builder(streamFactory, null);
}

public static Builder builder(IonFactory streamFactory, PropertyNamingStrategy strategy) {
return new Builder(new IonValueMapper(streamFactory, strategy));
}

/*
/**********************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
*/
public class IonValueMapperTest {
private final IonSystem ionSystem = IonSystemBuilder.standard().build();
private final IonValueMapper ionValueMapper = new IonValueMapper(ionSystem,
PropertyNamingStrategies.SNAKE_CASE);
private final IonValueMapper ionValueMapper = (IonValueMapper) IonValueMapper.builder(ionSystem,
PropertyNamingStrategies.SNAKE_CASE).build();

enum ReturnCode {
Success,
Expand Down Expand Up @@ -252,4 +252,16 @@ private void assertRoundTrip(String ion, Class<?> clazz) throws IOException {
IonValue actual = ionValueMapper.serialize(o);
assertEquals(expected, actual);
}

// for [dataformats-binary#509]
@Test
public void testBuilders() throws Exception {
IonObjectMapper mapper = IonValueMapper.builder().build();
assertNotNull(mapper);
assertEquals(IonValueMapper.class, mapper.getClass());

mapper = IonValueMapper.builder(ionSystem).build();
assertNotNull(mapper);
assertEquals(IonValueMapper.class, mapper.getClass());
}
}
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,7 @@ Joachim Lous (@jlous)
* Requested #494: Avro Schema generation: allow mapping Java Enum properties to
Avro String values
(2.18.0)

Robert Noack (@mr-robert)
* Reported #509: IonValueMapper.builder() not implemented, does not register modules
(2.18.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Active maintainers:
(contributed by Michal F)
#508: (avro) Ignore `specificData` field on serialization
(contributed by @pjfanning)
#509: IonValueMapper.builder() not implemented, does not register modules
(reported by Robert N)

2.17.3 (not yet released)

Expand Down

0 comments on commit 2fb7e1d

Please sign in to comment.