All Downloads are FREE. Search and download functionalities are using the official Maven repository.

datahub.protobuf.visitors.dataset.DeprecationVisitor Maven / Gradle / Ivy

There is a newer version: 0.14.1
Show newest version
package datahub.protobuf.visitors.dataset;

import com.google.protobuf.Descriptors;
import com.linkedin.common.Deprecation;
import com.linkedin.util.Pair;
import datahub.protobuf.visitors.ProtobufExtensionUtil;
import datahub.protobuf.visitors.ProtobufModelVisitor;
import datahub.protobuf.visitors.VisitContext;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static datahub.protobuf.ProtobufUtils.getMessageOptions;

public class DeprecationVisitor implements ProtobufModelVisitor {

    @Override
    public Stream visitGraph(VisitContext context) {
        if (context.root().messageProto().getOptions().getDeprecated()) {
            List> deprecationOptions = ProtobufExtensionUtil
                    .filterByDataHubType(getMessageOptions(context.root().messageProto()),
                            context.getGraph().getRegistry(), ProtobufExtensionUtil.DataHubMetadataType.DEPRECATION);

            String decommissionNote = deprecationOptions.stream()
                    .filter(opt -> opt.getKey().getJavaType() == Descriptors.FieldDescriptor.JavaType.STRING)
                    .flatMap(opt -> {
                        if (opt.getKey().isRepeated()) {
                            return ((Collection) opt.getValue()).stream();
                        } else {
                            return Stream.of(opt.getValue());
                        }
                    })
                    .map(Object::toString)
                    .collect(Collectors.joining("\n"));

            Optional decommissionTime = deprecationOptions.stream()
                    .filter(opt -> opt.getKey().getJavaType() == Descriptors.FieldDescriptor.JavaType.LONG)
                    .map(opt -> (Long) opt.getValue())
                    .findFirst();

            return Stream.of(new Deprecation()
                    .setDeprecated(true)
                    .setNote(decommissionNote)
                    .setDecommissionTime(decommissionTime.orElse(0L))
                    .setActor(context.getAuditStamp().getActor()));
        } else {
            return Stream.empty();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy