use of com.squareup.wire.schema.Options in project schema2proto by entur.
the class SchemaParser method processAttribute.
private void processAttribute(MessageType messageType, Set<Object> processedXmlObjects, XSAttributeUse attr) {
if (!processedXmlObjects.contains(attr)) {
processedXmlObjects.add(attr);
XSAttributeDecl decl = attr.getDecl();
XSSimpleType type = decl.getType();
if (type.getPrimitiveType() != null || type.isList() || type.isUnion()) {
String fieldName = decl.getName();
String doc = resolveDocumentationAnnotation(decl, false);
int tag = messageType.getNextFieldNum();
Location fieldLocation = getLocation(decl);
Options fieldOptions = getFieldOptions(decl);
String packageName = NamespaceHelper.xmlNamespaceToProtoFieldPackagename(type.getTargetNamespace(), configuration.forceProtoPackage);
Label label = type.isList() ? Label.REPEATED : null;
if (type.isRestriction() && type.getFacet(XSFacet.FACET_ENUMERATION) != null) {
String enumName = createEnum(fieldName, type.asRestriction(), decl.isLocal() ? messageType : null);
Field field = new Field(packageName, fieldLocation, label, fieldName, doc, tag, enumName, fieldOptions, false);
field.setFromAttribute(true);
addField(messageType, field);
} else {
String typeName = findFieldType(type);
Field field = new Field(basicTypes.contains(typeName) ? null : packageName, fieldLocation, label, fieldName, doc, tag, typeName, fieldOptions, false);
field.setFromAttribute(true);
addField(messageType, field);
}
} else {
LOGGER.error("Unhandled attribute use {}", attr.getDecl());
}
}
}
use of com.squareup.wire.schema.Options in project schema2proto by entur.
the class SchemaParser method createEnum.
private String createEnum(String elementName, XSRestrictionSimpleType type, MessageType enclosingType) {
Iterator<? extends XSFacet> it;
String typeNameToUse;
if (type.getName() != null) {
typeNameToUse = type.getName();
enclosingType = null;
} else {
String baseTypeName = type.getBaseType().getName();
if (baseTypeName != null && !basicTypes.contains(baseTypeName)) {
typeNameToUse = baseTypeName;
} else if (enclosingType != null) {
typeNameToUse = elementName + TYPE_SUFFIX;
} else {
typeNameToUse = elementName + GENERATED_NAME_PLACEHOLDER;
}
}
Type protoType = getType(type.getTargetNamespace(), typeNameToUse);
if (protoType == null) {
type = type.asRestriction();
Location location = getLocation(type);
List<EnumConstant> constants = new ArrayList<>();
it = type.getDeclaredFacets().iterator();
int counter = 1;
Set<String> addedValues = new HashSet<>();
while (it.hasNext()) {
List<OptionElement> optionElements = new ArrayList<>();
XSFacet next = it.next();
String doc = resolveDocumentationAnnotation(next, false);
String enumValue = next.getValue().value;
if (!addedValues.contains(enumValue)) {
addedValues.add(enumValue);
constants.add(new EnumConstant(location, enumValue, counter++, doc, new Options(Options.ENUM_VALUE_OPTIONS, optionElements)));
}
}
List<OptionElement> enumOptionElements = new ArrayList<>();
Options enumOptions = new Options(Options.ENUM_OPTIONS, enumOptionElements);
String doc = resolveDocumentationAnnotation(type, false);
ProtoType definedProtoType;
if (enclosingType == null) {
definedProtoType = ProtoType.get(typeNameToUse);
} else {
definedProtoType = ProtoType.get(enclosingType.getName(), typeNameToUse);
}
EnumType enumType = new EnumType(definedProtoType, location, doc, typeNameToUse, constants, new ArrayList<>(), enumOptions);
if (enclosingType != null) {
// if not already present
boolean alreadyPresentAsNestedType = false;
for (Type t : enclosingType.nestedTypes()) {
if (t instanceof EnumType && ((EnumType) t).name().equals(typeNameToUse)) {
alreadyPresentAsNestedType = true;
break;
}
}
if (!alreadyPresentAsNestedType) {
enclosingType.nestedTypes().add(enumType);
}
} else {
addType(type.getTargetNamespace(), enumType);
}
}
return typeNameToUse;
}
use of com.squareup.wire.schema.Options in project schema2proto by entur.
the class SchemaParser method createWrapperAndContinueProcessing.
private void createWrapperAndContinueProcessing(XSModelGroup modelGroup, XSParticle particle, MessageType messageType, Set<Object> processedXmlObjects, XSSchemaSet schemaSet, XSParticle[] children, String typeName, String targetNamespace, String fieldName, XSComplexType enclosingType) {
if (!processedXmlObjects.contains(particle)) {
processedXmlObjects.add(particle);
String enclosingName = typeName;
if (modelGroup.isModelGroupDecl()) {
enclosingName = modelGroup.asModelGroupDecl().getName();
}
// Create new message type enclosed in existing
String doc = resolveDocumentationAnnotation(modelGroup, false);
Location location = getLocation(modelGroup);
// Add message type to file
List<OptionElement> messageOptions = new ArrayList<>();
Options options = new Options(Options.MESSAGE_OPTIONS, messageOptions);
MessageType wrapperType = new MessageType(ProtoType.get(typeName), location, doc, typeName, options);
wrapperType.setWrapperMessageType(true);
messageType.nestedTypes().add(wrapperType);
Options fieldOptions = getFieldOptions(particle);
String fieldPackagename = NamespaceHelper.xmlNamespaceToProtoFieldPackagename(targetNamespace, configuration.forceProtoPackage);
Field field = new Field(fieldPackagename, location, Label.REPEATED, fieldName, doc, messageType.getNextFieldNum(), typeName, fieldOptions, false);
addField(messageType, field);
localTypes.add(new LocalType(particle, wrapperType, messageType, field, NamespaceHelper.xmlNamespaceToProtoPackage(targetNamespace, configuration.forceProtoPackage), enclosingType));
processGroupAsSequence(particle, wrapperType, processedXmlObjects, schemaSet, children, enclosingName, targetNamespace, enclosingType);
}
}
use of com.squareup.wire.schema.Options in project schema2proto by entur.
the class SerializationTest method testBuildExtension.
@Test
public void testBuildExtension() {
ProtoFile f = new ProtoFile(Syntax.PROTO_3, "default");
Location loc = new Location("", "", 0, 0);
List<Field> fields = new ArrayList<>();
List<OptionElement> optionElements = new ArrayList<>();
Options options = new Options(ProtoType.get("google.protobuf.MessageOptions"), optionElements);
Field field = new Field(null, loc, null, "fieldname", "Base type this message actually is an extension of", 1101, "string", options, false);
fields.add(field);
Extend e = new Extend(loc, "Information elements extracted from the xsd structure", "google.protobuf.MessageOptions", fields);
f.getExtendList().add(e);
String schema = f.toSchema();
System.out.println(schema);
assertNotNull(schema);
}
use of com.squareup.wire.schema.Options in project schema2proto by entur.
the class SerializationTest method testBuildProtoFile.
@Test
public void testBuildProtoFile() {
ProtoFile f = new ProtoFile(Syntax.PROTO_3, "default");
List<EnumConstant> values = new ArrayList<EnumConstant>();
List<OptionElement> elements = new ArrayList<>();
List<Reserved> reserveds = new ArrayList<>();
Options constantOptions = new Options(ProtoType.BOOL, elements);
Location valueLocation = new Location("base", "file", 1, 2);
values.add(new EnumConstant(valueLocation, "VAL1", 1, "doc", constantOptions));
Options options = new Options(ProtoType.BOOL, new ArrayList<>());
f.types().add(new EnumType(ProtoType.INT32, valueLocation, "doc", "Name", values, reserveds, options));
Location messagLocation = new Location("base", "path", 1, 1);
ImmutableList<Field> declaredFields = ImmutableList.copyOf(new ArrayList<>());
List<Field> extensionFields = new ArrayList<>();
ImmutableList<OneOf> oneOfs = ImmutableList.copyOf(new ArrayList<>());
ImmutableList<Type> nestedTypes = ImmutableList.copyOf(new ArrayList<>());
List<Extensions> extensionsList = new ArrayList<>();
List<OptionElement> messageOptionElements = new ArrayList<>();
Options messageOptions = new Options(ProtoType.BOOL, messageOptionElements);
MessageType message = new MessageType(ProtoType.BOOL, messagLocation, "doc", "messagename", declaredFields, extensionFields, oneOfs, nestedTypes, extensionsList, reserveds, messageOptions);
f.types().add(message);
String schema = f.toSchema();
assertNotNull(schema);
}
Aggregations