Java Code Examples for com.google.protobuf.Descriptors#OneofDescriptor

The following examples show how to use com.google.protobuf.Descriptors#OneofDescriptor . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: xresloader   File: DataVerifyPbOneof.java    License: MIT License 6 votes vote down vote up
public DataVerifyPbOneof(Descriptors.OneofDescriptor desc) {
    super(desc.getName());

    for (Descriptors.FieldDescriptor fd : desc.getFields()) {

        all_names.put(fd.getName(), (long) fd.getNumber());
        all_numbers.add((long) fd.getNumber());

        // alias extension
        if (fd.getOptions().hasExtension(Xresloader.fieldAlias)) {
            String alias_name = fd.getOptions().getExtension(Xresloader.fieldAlias);
            if (!alias_name.isEmpty()) {
                all_names.put(alias_name, (long) fd.getNumber());
            }
        }
    }
}
 
Example 2
Source Project: xresloader   File: DataDstPbHelper.java    License: MIT License 6 votes vote down vote up
static public HashMap<String, Object> dumpOptionsIntoHashMap(Descriptors.OneofDescriptor msg_desc,
        com.google.protobuf.ExtensionRegistry custom_extensions) {
    HashMap<String, Object> msg_root = new HashMap<String, Object>();
    boolean has_data = false;

    HashMap<String, Object> options_data = dumpMessageExtensions(msg_desc.getOptions(),
            msg_desc.getOptions().getAllFields(), custom_extensions);
    if (options_data != null && !options_data.isEmpty()) {
        has_data = true;
        msg_root.put("options", options_data);
    }

    if (has_data) {
        msg_root.put("name", msg_desc.getName());
        return msg_root;
    }

    return null;
}
 
Example 3
Source Project: xresloader   File: DataDstPb.java    License: MIT License 6 votes vote down vote up
static private void setup_extension(DataDstOneofDescriptor child_field, Descriptors.Descriptor container,
        Descriptors.OneofDescriptor fd) {
    LinkedList<DataVerifyImpl> gen = setup_verifier(container, fd);
    if (gen != null && !gen.isEmpty()) {
        for (DataVerifyImpl vfy : gen) {
            child_field.addVerifier(vfy);
        }
    } else {
        child_field.resetVerifier();
    }

    if (fd.getOptions().hasExtension(Xresloader.oneofDescription)) {
        child_field.mutableExtension().description = fd.getOptions().getExtension(Xresloader.oneofDescription);
    }

    if (fd.getOptions().hasExtension(Xresloader.oneofSeparator)) {
        child_field.mutableExtension().plainSeparator = fd.getOptions().getExtension(Xresloader.oneofSeparator);
    }
}
 
Example 4
Source Project: xresloader   File: DataDstPb.java    License: MIT License 6 votes vote down vote up
static private LinkedList<DataVerifyImpl> setup_verifier(Descriptors.Descriptor container,
        Descriptors.OneofDescriptor fd) {
    LinkedList<DataVerifyImpl> ret = new LinkedList<DataVerifyImpl>();

    String rule = String.format("%s.%s.%s", container.getFile().getPackage(), container.getName(), fd.getName());
    if (rule.length() > 0 && rule.charAt(0) == '.') {
        rule = rule.substring(1);
    }
    {
        DataVerifyImpl vfy = cachePbs.identifiers.getOrDefault(rule, null);
        // 命中缓存
        if (null != vfy) {
            ret.add(vfy);
            return ret;
        }
    }

    DataVerifyPbOneof new_vfy = new DataVerifyPbOneof(fd);
    cachePbs.identifiers.put(rule, new_vfy);
    ret.add(new_vfy);

    return ret;
}
 
Example 5
Source Project: xresloader   File: DataDstPb.java    License: MIT License 6 votes vote down vote up
private void setup_node_identify(DataDstWriterNode node, DataDstChildrenNode child, IdentifyDescriptor identify,
        Descriptors.OneofDescriptor fd) {
    node.identify = identify;

    identify.referToWriterNode = node;
    identify.resetVerifier();

    // Data source field verifier is ignored in oneof descriptor

    // merge verifier from oneof descriptor
    if (child.innerOneofDesc != null && child.innerOneofDesc.hasVerifier()) {
        for (DataVerifyImpl vfy : child.innerOneofDesc.getVerifier()) {
            identify.addVerifier(vfy);
        }
    }
}
 
Example 6
Source Project: xresloader   File: DataDstPb.java    License: MIT License 6 votes vote down vote up
private void filterMissingFields(LinkedList<String> missingFields, HashMap<String, String> oneofField,
        Descriptors.FieldDescriptor fd, boolean isMissing) throws ConvException {
    if (missingFields == null && oneofField == null) {
        return;
    }

    Descriptors.OneofDescriptor oneof = fd.getContainingOneof();
    if (isMissing && oneof == null && missingFields != null) {
        missingFields.push(fd.getName());
    }

    if (!isMissing && oneof != null && oneofField.containsKey(oneof.getFullName())) {
        String old_field = oneofField.get(oneof.getFullName());
        if (old_field != null) {
            setLastErrorMessage(
                    "field \"%s\" in oneof descriptor \"%s\" already exists, can not add another field \"%s\" with the same oneof descriptor",
                    old_field, oneof.getFullName(), fd.getName());
            throw new ConvException(getLastErrorMessage());
        }
        oneofField.replace(oneof.getFullName(), fd.getName());
    }
}
 
Example 7
Source Project: xresloader   File: DataDstPb.java    License: MIT License 6 votes vote down vote up
private boolean dumpPlainField(DynamicMessage.Builder builder, IdentifyDescriptor ident,
        DataDstWriterNode.DataDstOneofDescriptor field, boolean isTopLevel) throws ConvException {
    if (field == null) {
        return false;
    }

    if (null == ident) {
        if (ProgramOptions.getInstance().enbleEmptyList) {
            Descriptors.OneofDescriptor fd = (Descriptors.OneofDescriptor) field.getRawDescriptor();
            if (null == fd) {
                // 不需要提示,如果从其他方式解包协议描述的时候可能有可选字段丢失的
                return false;
            }
            dumpDefault(builder, fd);
        }
        return false;
    }

    DataContainer<String> res = DataSrcImpl.getOurInstance().getValue(ident, "");
    if (null == res || !res.valid) {
        return false;
    }

    return dumpPlainField(builder, ident, field, isTopLevel, res.value);
}
 
Example 8
protected void validateUnionDescriptor(Descriptors.Descriptor unionDescriptor) {
    final List<Descriptors.OneofDescriptor> oneofs = unionDescriptor.getOneofs();
    if (!oneofs.isEmpty()) {
        if (oneofs.size() > 1) {
            throw new MetaDataException("Union descriptor has more than one oneof");
        }
        if (oneofs.get(0).getFieldCount() != unionDescriptor.getFields().size()) {
            throw new MetaDataException("Union descriptor oneof must contain every field");
        }
    }
}
 
Example 9
Source Project: xresloader   File: DataDstPbHelper.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
static public void dumpConstIntoHashMap(String package_name, HashMap<String, Object> parent,
        Descriptors.OneofDescriptor oneof_desc) {
    String oneof_seg = oneof_desc.getName();
    HashMap<String, Object> oneof_root;
    if (parent.containsKey(oneof_seg)) {
        Object node = parent.get(oneof_seg);
        if (node instanceof HashMap) {
            oneof_root = (HashMap<String, Object>) node;
        } else {
            ProgramOptions.getLoger().error("oneof name %s.%s conflict.", package_name, oneof_seg);
            return;
        }
    } else {
        oneof_root = new HashMap<String, Object>();
        parent.put(oneof_seg, oneof_root);
    }

    // 写出所有常量值
    for (Descriptors.FieldDescriptor oneof_option : oneof_desc.getFields()) {
        String field_name = oneof_option.getJsonName();
        if (field_name.length() > 0) {
            field_name = Character.toUpperCase(field_name.charAt(0)) + field_name.substring(1);
        }
        field_name = String.format("k%s", field_name);
        oneof_root.put(field_name, oneof_option.getNumber());
    }
}
 
Example 10
Source Project: xresloader   File: DataDstPbHelper.java    License: MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
static public void dumpConstIntoHashMap(String package_name, HashMap<String, Object> parent,
        Descriptors.Descriptor msg_desc) {
    String msg_seg = msg_desc.getName();
    HashMap<String, Object> msg_root = null;
    Object msg_node = parent.getOrDefault(msg_seg, null);
    String msg_full_name = String.format("%s.%s", package_name, msg_seg);
    if (msg_node != null) {
        if (msg_node instanceof HashMap) {
            msg_root = (HashMap<String, Object>) msg_node;
        } else {
            ProgramOptions.getLoger().error("message name %s conflict.", msg_full_name);
            return;
        }
    }

    // enum in message.
    for (Descriptors.EnumDescriptor enum_desc : msg_desc.getEnumTypes()) {
        if (null == msg_root) {
            msg_root = new HashMap<String, Object>();
            parent.put(msg_seg, msg_root);
        }

        dumpConstIntoHashMap(msg_full_name, msg_root, enum_desc);
    }

    // if has oneof in message, dump all fields's number.
    for (Descriptors.OneofDescriptor oneof_desc : msg_desc.getOneofs()) {
        if (oneof_desc.getFieldCount() <= 0) {
            continue;
        }
        if (null == msg_root) {
            msg_root = new HashMap<String, Object>();
            parent.put(msg_seg, msg_root);
        }

        dumpConstIntoHashMap(msg_full_name, msg_root, oneof_desc);
    }

    // nested message
    for (Descriptors.Descriptor sub_msg_desc : msg_desc.getNestedTypes()) {
        if (null == msg_root) {
            msg_root = new HashMap<String, Object>();
            parent.put(msg_seg, msg_root);
        }

        dumpConstIntoHashMap(msg_full_name, msg_root, sub_msg_desc);
    }
}
 
Example 11
Source Project: xresloader   File: DataDstPb.java    License: MIT License 4 votes vote down vote up
private void dumpDefault(DynamicMessage.Builder builder, Descriptors.OneofDescriptor fd) {
    builder.clearOneof(fd);
}