fixedformat4j icon indicating copy to clipboard operation
fixedformat4j copied to clipboard

Nested Record sample doesnt work

Open StevenGerdes opened this issue 7 years ago • 6 comments

com.ancientprogramming.fixedformat4j.exception.FixedFormatException: com.ancientprogramming.fixedformat4j.format.impl.ByTypeFormatter cannot handle datatype[com.foo.exampleRecord]. Provide your own custom FixedFormatter for this datatype.

StevenGerdes avatar Jun 06 '18 22:06 StevenGerdes

@StevenGerdes got the same issue here. As a workaround, I implemented the FixedFormatter on every possible sub-record, eg.:

In my parent record: @Field(offset = 31, length = 100, formatter = SubRecord.class) public SubRecord getSubRecord() { .. }

In my sub record: ` @Record(length = 100) public class SubRecord implements FixedFormatter {

String a = "foo";
String b = "bar";

@Field(offset = 1, length = 40)
public String getA() {
    return a;
}

@Field(offset = 41, length = 60)
public String getB() {
    return b;
}
@Override
public Object parse(String s, FormatInstructions formatInstructions) throws FixedFormatException {
    return null;
}

@Override
public String format(Object o, FormatInstructions formatInstructions) throws FixedFormatException {
    FixedFormatManager manager = new FixedFormatManagerImpl();
    return manager.export(this);
}

}`

davidcyp avatar Jul 12 '18 15:07 davidcyp

@StevenGerdes .. and encapsulated it in an abstract super class:

`public abstract class AbstractSubRecordFixedFormatter implements FixedFormatter {

@Override
public Object parse(String s, FormatInstructions formatInstructions) throws FixedFormatException {
    return null;
}

@Override
public String format(Object o, FormatInstructions formatInstructions) throws FixedFormatException {
    FixedFormatManager manager = new FixedFormatManagerImpl();
    return manager.export(this);
}

}`

which now is the base class for all my records

davidcyp avatar Jul 12 '18 16:07 davidcyp

Apparently there were still some more problems. However, I found out the maven repo only hosts up to version 1.2.2. If you download the source code and build it yourself, you will get version 1.4.0-SNAPSHOT, which is working fine.

davidcyp avatar Jul 13 '18 09:07 davidcyp

@davidcyp

Please check my work on https://github.com/ty-vincenthung/fixedformatter4j/tree/1.0-snapshot

it is based on version 1.2.2 and all nested need to be defined @Record and @Field

ty-vincenthung avatar Feb 09 '21 08:02 ty-vincenthung

Thanks @ty-vincenthung I'll do asap. Not working on the project anymore so need to do some setup but really thanks for the answer.

davidcyp avatar Feb 09 '21 08:02 davidcyp

I have updated the formatter which support Generic Object, List and Enum

ty-vincenthung avatar Feb 26 '21 13:02 ty-vincenthung