Search in sources :

Example 1 with PPrimitive

use of net.morimekta.providence.descriptor.PPrimitive in project providence by morimekta.

the class BaseTypeRegistry method getProvider.

@Nonnull
@Override
@SuppressWarnings("unchecked")
public PDescriptorProvider getProvider(@Nonnull String name, @Nonnull final String context, @Nullable Map<String, String> annotations) {
    name = finalTypename(name, context);
    // Prepend package context to name
    PPrimitive primitive = PPrimitive.findByName(name);
    if (primitive != null) {
        return primitive.provider();
    }
    if (annotations == null) {
        annotations = Collections.EMPTY_MAP;
    }
    // Collection types are a bit complex, so handle it here.
    if (name.startsWith("map<") && name.endsWith(">")) {
        String[] parts = name.substring(4, name.length() - 1).split(",", 2);
        String keyType = parts[0];
        String valueType = parts[1];
        switch(ThriftContainer.forName(annotations.get(ThriftAnnotation.CONTAINER.tag))) {
            case SORTED:
                return PMap.sortedProvider(getProvider(keyType, context, null), getProvider(valueType, context, null));
            case ORDERED:
                return PMap.orderedProvider(getProvider(keyType, context, null), getProvider(valueType, context, null));
            case DEFAULT:
                return PMap.provider(getProvider(keyType, context, null), getProvider(valueType, context, null));
        }
    }
    if (name.startsWith("set<") && name.endsWith(">")) {
        String itemType = name.substring(4, name.length() - 1);
        switch(ThriftContainer.forName(annotations.get(ThriftAnnotation.CONTAINER.tag))) {
            case SORTED:
                return PSet.sortedProvider(getProvider(itemType, context, null));
            case ORDERED:
                return PSet.orderedProvider(getProvider(itemType, context, null));
            case DEFAULT:
                return PSet.provider(getProvider(itemType, context, null));
        }
    }
    if (name.startsWith("list<") && name.endsWith(">")) {
        String itemType = name.substring(5, name.length() - 1);
        return PList.provider(getProvider(itemType, context, null));
    }
    // Otherwise it's a declared type.
    final String finalName = name;
    return () -> getDeclaredType(finalName, context);
}
Also used : PPrimitive(net.morimekta.providence.descriptor.PPrimitive) Nonnull(javax.annotation.Nonnull)

Example 2 with PPrimitive

use of net.morimekta.providence.descriptor.PPrimitive in project providence by morimekta.

the class BuilderCommonMemberFormatter method appendMutableGetters.

/**
 * Get mutable values. This returns message builders for messages, collection
 * builders for collections, and the normal immutable value for everything
 * else.
 *
 * @param message The message to get mutable getters for.
 * @param field The field to generate getter for.
 */
private void appendMutableGetters(JMessage message, JField field) throws GeneratorException {
    if (field.field().getDescriptor() instanceof PPrimitive || field.type() == PType.ENUM) {
        // The other fields will have ordinary non-mutable getters.
        appendGetter(field);
        return;
    }
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    if (field.hasComment()) {
        comment.comment(field.comment());
    } else {
        comment.comment("Gets the builder for the contained " + field.name() + ".");
    }
    comment.newline().return_("The field builder");
    if (JAnnotation.isDeprecated(field)) {
        String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
        if (reason != null && reason.trim().length() > 0) {
            comment.deprecated_(reason);
        }
    }
    comment.finish();
    if (JAnnotation.isDeprecated(field)) {
        writer.appendln(JAnnotation.DEPRECATED);
    }
    writer.appendln(JAnnotation.NON_NULL);
    switch(field.type()) {
        case MESSAGE:
            {
                writer.formatln("public %s._Builder %s() {", field.instanceType(), field.mutable()).begin();
                if (message.isUnion()) {
                    writer.formatln("if (%s != _Field.%s) {", UNION_FIELD, field.fieldEnum()).formatln("    %s();", field.resetter()).appendln('}').formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
                    writer.appendln("modified = true;");
                } else {
                    writer.formatln("optionals.set(%d);", field.index());
                    writer.formatln("modified.set(%d);", field.index());
                }
                writer.newline().formatln("if (%s != null) {", field.member()).formatln("    %s_builder = %s.mutate();", field.member(), field.member()).formatln("    %s = null;", field.member()).formatln("} else if (%s_builder == null) {", field.member()).formatln("    %s_builder = %s.builder();", field.member(), field.instanceType()).appendln('}').formatln("return %s_builder;", field.member());
                writer.end().appendln('}').newline();
                // Also add "normal" getter for the message field, which will
                // return the message or the builder dependent on which is set.
                // It will not change the state of the builder.
                comment = new BlockCommentBuilder(writer);
                if (field.hasComment()) {
                    comment.comment(field.comment());
                } else {
                    comment.comment("Gets the value for the contained " + field.name() + ".");
                }
                comment.newline().return_("The field value");
                if (JAnnotation.isDeprecated(field)) {
                    String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
                    if (reason != null && reason.trim().length() > 0) {
                        comment.deprecated_(reason);
                    }
                }
                comment.finish();
                if (JAnnotation.isDeprecated(field)) {
                    writer.appendln(JAnnotation.DEPRECATED);
                }
                writer.formatln("public %s %s() {", field.instanceType(), field.getter()).begin();
                if (message.isUnion()) {
                    writer.formatln("if (%s != _Field.%s) {", UNION_FIELD, field.fieldEnum()).formatln("    return null;").appendln('}');
                }
                writer.newline().formatln("if (%s_builder != null) {", field.member()).formatln("    return %s_builder.build();", field.member()).appendln('}').formatln("return %s;", field.member());
                writer.end().appendln('}').newline();
                break;
            }
        case SET:
        case LIST:
        case MAP:
            writer.formatln("public %s %s() {", field.fieldType(), field.mutable()).begin();
            if (message.isUnion()) {
                writer.formatln("if (%s != _Field.%s) {", UNION_FIELD, field.fieldEnum()).formatln("    %s();", field.resetter()).appendln('}').formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
                writer.appendln("modified = true;");
            } else {
                writer.formatln("optionals.set(%d);", field.index());
                writer.formatln("modified.set(%d);", field.index());
            }
            writer.newline().formatln("if (%s == null) {", field.member()).formatln("    %s = new %s<>();", field.member(), field.builderMutableType()).formatln("} else if (!(%s instanceof %s)) {", field.member(), field.builderMutableType()).formatln("    %s = new %s<>(%s);", field.member(), field.builderMutableType(), field.member()).appendln("}");
            writer.formatln("return %s;", field.member());
            writer.end().appendln('}').newline();
            break;
        default:
            throw new GeneratorException("Unexpected field type: " + field.type());
    }
}
Also used : BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder) GeneratorException(net.morimekta.providence.generator.GeneratorException) PPrimitive(net.morimekta.providence.descriptor.PPrimitive)

Example 3 with PPrimitive

use of net.morimekta.providence.descriptor.PPrimitive in project providence by morimekta.

the class JField method getProvider.

public String getProvider() throws GeneratorException {
    String container = field.getAnnotationValue("container");
    String containerProvider = "provider";
    if (container != null) {
        ThriftContainer containerType = ThriftContainer.forName(container);
        switch(containerType) {
            case DEFAULT:
                containerProvider = "provider";
                break;
            case SORTED:
                containerProvider = "sortedProvider";
                break;
            case ORDERED:
                containerProvider = "orderedProvider";
                break;
            default:
                break;
        }
    }
    switch(field.getType()) {
        case ENUM:
        case MESSAGE:
            return String.format(Locale.US, "%s.provider()", helper.getFieldType(field.getDescriptor()));
        case LIST:
            PList<?> lType = (PList<?>) field.getDescriptor();
            return String.format(Locale.US, "%s.provider(%s)", PList.class.getName(), helper.getProviderName(lType.itemDescriptor()));
        case SET:
            PSet<?> sType = (PSet<?>) field.getDescriptor();
            return String.format(Locale.US, "%s.%s(%s)", PSet.class.getName(), containerProvider, helper.getProviderName(sType.itemDescriptor()));
        case MAP:
            PMap<?, ?> mType = (PMap<?, ?>) field.getDescriptor();
            return String.format(Locale.US, "%s.%s(%s,%s)", PMap.class.getName(), containerProvider, helper.getProviderName(mType.keyDescriptor()), helper.getProviderName(mType.itemDescriptor()));
        default:
            if (!(field.getDescriptor() instanceof PPrimitive)) {
                throw new IllegalArgumentException("Unhandled type group " + field.getType());
            }
            return String.format(Locale.US, "%s.%s.provider()", PPrimitive.class.getName(), field.getDescriptor().getName().toUpperCase(Locale.US));
    }
}
Also used : ThriftContainer(net.morimekta.providence.util.ThriftContainer) PList(net.morimekta.providence.descriptor.PList) PSet(net.morimekta.providence.descriptor.PSet) PMap(net.morimekta.providence.descriptor.PMap) PPrimitive(net.morimekta.providence.descriptor.PPrimitive)

Aggregations

PPrimitive (net.morimekta.providence.descriptor.PPrimitive)3 Nonnull (javax.annotation.Nonnull)1 PList (net.morimekta.providence.descriptor.PList)1 PMap (net.morimekta.providence.descriptor.PMap)1 PSet (net.morimekta.providence.descriptor.PSet)1 GeneratorException (net.morimekta.providence.generator.GeneratorException)1 BlockCommentBuilder (net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)1 ThriftContainer (net.morimekta.providence.util.ThriftContainer)1