add AnyStruct

This commit is contained in:
Vaci Koblizek 2020-11-09 15:43:23 +00:00
parent e04adc90b6
commit cefd8eaaa2
5 changed files with 87 additions and 13 deletions

View file

@ -83,13 +83,13 @@
<phase>generate-test-sources</phase> <phase>generate-test-sources</phase>
<configuration> <configuration>
<target> <target>
<mkdir dir="src/test/generated"/> <mkdir dir="src/test/generated/org/capnproto/test"/>
<exec executable="capnp" failonerror="true"> <exec executable="capnp" failonerror="true">
<arg value="compile"/> <arg value="compile"/>
<arg value="-I"/> <arg value="-I"/>
<arg value="src/main/schema/"/> <arg value="src/main/schema/"/>
<arg value="--src-prefix=src/test/schema/"/> <arg value="--src-prefix=src/test/schema/"/>
<arg value="-o../capnpc-java:src/test/generated"/> <arg value="-o../capnpc-java:src/test/generated/org/capnproto/test"/>
<arg value="src/test/schema/test.capnp"/> <arg value="src/test/schema/test.capnp"/>
<arg value="src/test/schema/test-import.capnp"/> <arg value="src/test/schema/test-import.capnp"/>
<env key="CAPNP_LITE" value="1"/> <env key="CAPNP_LITE" value="1"/>

View file

@ -412,6 +412,8 @@ private:
switch (type.whichAnyPointerKind()) { switch (type.whichAnyPointerKind()) {
case schema::Type::AnyPointer::Unconstrained::CAPABILITY: case schema::Type::AnyPointer::Unconstrained::CAPABILITY:
return kj::strTree("org.capnproto.Capability.", suffix); return kj::strTree("org.capnproto.Capability.", suffix);
case schema::Type::AnyPointer::Unconstrained::STRUCT:
return kj::strTree("org.capnproto.AnyStruct.", suffix);
default: default:
return kj::strTree("org.capnproto.AnyPointer.", suffix); return kj::strTree("org.capnproto.AnyPointer.", suffix);
} }
@ -775,8 +777,10 @@ private:
} else { } else {
switch (type.whichAnyPointerKind()) { switch (type.whichAnyPointerKind()) {
case schema::Type::AnyPointer::Unconstrained::CAPABILITY: case schema::Type::AnyPointer::Unconstrained::CAPABILITY:
return kj::str("org.capnproto.Capability.factory"); return kj::str("org.capnproto.Capability.factory");
case schema::Type::AnyPointer::Unconstrained::STRUCT:
return kj::str("org.capnproto.AnyStruct.factory");
default: default:
return kj::str("org.capnproto.AnyPointer.factory"); return kj::str("org.capnproto.AnyPointer.factory");
} }
@ -1035,7 +1039,7 @@ private:
kind = FieldKind::ANY_POINTER; kind = FieldKind::ANY_POINTER;
break; break;
case schema::Type::AnyPointer::Unconstrained::STRUCT: case schema::Type::AnyPointer::Unconstrained::STRUCT:
kind = FieldKind::STRUCT; kind = FieldKind::ANY_POINTER;
break; break;
case schema::Type::AnyPointer::Unconstrained::LIST: case schema::Type::AnyPointer::Unconstrained::LIST:
kind = FieldKind::LIST; kind = FieldKind::LIST;
@ -1967,14 +1971,14 @@ private:
} }
if (resultProto.getIsGeneric()) { if (resultProto.getIsGeneric()) {
auto resultFactoryArgs = getFactoryArguments(resultSchema, paramSchema); auto resultFactoryArgs = getFactoryArguments(resultSchema, paramSchema);
resultFactory = resultFactoryArgs.size() == 0 resultFactory = resultFactoryArgs.size() == 0
? kj::str(shortResultType, ".factory") ? kj::str(shortResultType, ".factory")
: kj::strTree("newFactory(", : kj::strTree("newFactory(",
kj::StringTree(KJ_MAP(arg, resultFactoryArgs) { kj::StringTree(KJ_MAP(arg, resultFactoryArgs) {
return kj::strTree(arg); return kj::strTree(arg);
}, ", "), }, ", "),
")").flatten(); ")").flatten();
} }
auto paramBuilder = kj::str(shortParamType, ".Builder"); auto paramBuilder = kj::str(shortParamType, ".Builder");

View file

@ -1,5 +1,6 @@
package org.capnproto.test; package org.capnproto.test;
import org.capnproto.test.Test;
import org.capnproto.*; import org.capnproto.*;
import org.capnproto.Void; import org.capnproto.Void;
import org.junit.Assert; import org.junit.Assert;
@ -845,6 +846,13 @@ public class EncodingTest {
TestUtil.checkTestMessage(listReader.get(1)); TestUtil.checkTestMessage(listReader.get(1));
} }
@org.junit.Test
public void testAnyStruct() {
MessageBuilder builder = new MessageBuilder();
var root = builder.initRoot(Test.TestAnyOthers.factory);
var anyStruct = root.initAnyStructField();
}
@org.junit.Test @org.junit.Test
public void testCopyAnyPointer() { public void testCopyAnyPointer() {
MessageBuilder message1 = new MessageBuilder(); MessageBuilder message1 = new MessageBuilder();

View file

@ -148,6 +148,12 @@ struct TestAnyPointer {
# in the struct. # in the struct.
} }
struct TestAnyOthers {
anyStructField @0 :AnyStruct;
#anyListField @1 :AnyPointer; # not currently implemented
#capabilityField @2 :Capability;
}
struct TestOutOfOrder { struct TestOutOfOrder {
foo @3 :Text; foo @3 :Text;
bar @2 :Text; bar @2 :Text;

View file

@ -0,0 +1,56 @@
package org.capnproto;
public class AnyStruct {
public static final org.capnproto.StructSize STRUCT_SIZE = new org.capnproto.StructSize((short)0,(short)0);
public static final class Factory extends org.capnproto.StructFactory<Builder, Reader> {
public Factory() {
}
public final Reader constructReader(org.capnproto.SegmentReader segment, int data,int pointers, int dataSize, short pointerCount, int nestingLimit) {
return new Reader(segment,data,pointers,dataSize,pointerCount,nestingLimit);
}
public final Builder constructBuilder(org.capnproto.SegmentBuilder segment, int data,int pointers, int dataSize, short pointerCount) {
return new Builder(segment, data, pointers, dataSize, pointerCount);
}
public final org.capnproto.StructSize structSize() {
return AnyStruct.STRUCT_SIZE;
}
public final Reader asReader(Builder builder) {
return builder.asReader();
}
}
public static final Factory factory = new Factory();
public static final org.capnproto.StructList.Factory<Builder,Reader> listFactory =
new org.capnproto.StructList.Factory<>(factory);
public static final class Builder extends org.capnproto.StructBuilder {
Builder(org.capnproto.SegmentBuilder segment, int data, int pointers,int dataSize, short pointerCount){
super(segment, data, pointers, dataSize, pointerCount);
}
public final Reader asReader() {
return new Reader(segment, data, pointers, dataSize, pointerCount, 0x7fffffff);
}
public final <T> T initAs(StructBuilder.Factory<T> factory) {
return factory.constructBuilder(this.segment, this.capTable, this.data, this.pointers, this.dataSize, this.pointerCount);
}
public final <T> T setAs(StructBuilder.Factory<T> factory) {
return factory.constructBuilder(this.segment, this.capTable, this.data, this.pointers, this.dataSize, this.pointerCount);
}
}
public static final class Reader extends org.capnproto.StructReader {
Reader(org.capnproto.SegmentReader segment, int data, int pointers,int dataSize, short pointerCount, int nestingLimit){
super(segment, data, pointers, dataSize, pointerCount, nestingLimit);
}
public final <T> T getAs(StructReader.Factory<T> factory) {
return factory.constructReader(this.segment, this.capTable, this.data, this.pointers, this.dataSize, this.pointerCount, this.nestingLimit);
}
}
}