Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.druid.data.input.protobuf;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.protobuf.Any;
import com.google.protobuf.BoolValue;
import com.google.protobuf.ByteString;
import com.google.protobuf.BytesValue;
import com.google.protobuf.Descriptors;
import com.google.protobuf.DoubleValue;
import com.google.protobuf.Duration;
import com.google.protobuf.FieldMask;
import com.google.protobuf.FloatValue;
import com.google.protobuf.Int32Value;
import com.google.protobuf.Int64Value;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.ListValue;
import com.google.protobuf.Message;
import com.google.protobuf.StringValue;
import com.google.protobuf.Struct;
import com.google.protobuf.Timestamp;
import com.google.protobuf.UInt32Value;
import com.google.protobuf.UInt64Value;
import com.google.protobuf.Value;
import com.google.protobuf.util.Durations;
import com.google.protobuf.util.FieldMaskUtil;
import com.google.protobuf.util.JsonFormat;
import com.google.protobuf.util.Timestamps;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Convert {@link Message} to plain java stuffs, based roughly on the conversions done with {@link JsonFormat}
*/
public class ProtobufConverter
{
private static final Map SPECIAL_CONVERSIONS = buildSpecializedConversions();
@Nullable
public static Map convertMessage(Message msg) throws InvalidProtocolBufferException
{
if (msg == null) {
return null;
}
final Map fields = msg.getAllFields();
final Map converted = Maps.newHashMapWithExpectedSize(fields.size());
for (Map.Entry field : fields.entrySet()) {
converted.put(field.getKey().getJsonName(), convertField(field.getKey(), field.getValue()));
}
return converted;
}
@Nullable
private static Object convertField(Descriptors.FieldDescriptor field, Object value)
throws InvalidProtocolBufferException
{
// handle special types
if (value instanceof Message) {
Message msg = (Message) value;
final String typeName = msg.getDescriptorForType().getFullName();
SpecializedConverter converter = SPECIAL_CONVERSIONS.get(typeName);
if (converter != null) {
return converter.convert(msg);
}
}
if (field.isMapField()) {
return convertMap(field, value);
} else if (field.isRepeated()) {
return convertList(field, (List>) value);
} else {
return convertSingleValue(field, value);
}
}
@Nonnull
private static List