MessageNamedArgumentFinder.java

  1. /*
  2.  * Copyright 2018-2019 Providence Authors
  3.  *
  4.  * Licensed to the Apache Software Foundation (ASF) under one
  5.  * or more contributor license agreements. See the NOTICE file
  6.  * distributed with this work for additional information
  7.  * regarding copyright ownership. The ASF licenses this file
  8.  * to you under the Apache License, Version 2.0 (the
  9.  * "License"); you may not use this file except in compliance
  10.  * with the License. You may obtain a copy of the License at
  11.  *
  12.  *   http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing,
  15.  * software distributed under the License is distributed on an
  16.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17.  * KIND, either express or implied. See the License for the
  18.  * specific language governing permissions and limitations
  19.  * under the License.
  20.  */
  21. package net.morimekta.providence.jdbi.v2;

  22. import net.morimekta.providence.PMessage;
  23. import net.morimekta.providence.PMessageOrBuilder;
  24. import net.morimekta.providence.PType;
  25. import net.morimekta.providence.descriptor.PField;
  26. import net.morimekta.providence.descriptor.PMessageDescriptor;
  27. import net.morimekta.providence.jdbi.v2.util.NullArgument;
  28. import net.morimekta.util.collect.UnmodifiableMap;
  29. import org.skife.jdbi.v2.tweak.Argument;
  30. import org.skife.jdbi.v2.tweak.NamedArgumentFinder;

  31. import javax.annotation.Nonnull;
  32. import javax.annotation.Nullable;
  33. import java.util.Map;

  34. import static net.morimekta.util.Strings.isNullOrEmpty;

  35. /**
  36.  * A {@link NamedArgumentFinder} implementation that uses a message
  37.  * and finds values based on the thrift declared field names. This
  38.  * supports chained calls to any depth as long as each level is a
  39.  * single message field.
  40.  *
  41.  * @param <M> The message type.
  42.  */
  43. public class MessageNamedArgumentFinder<M extends PMessage<M>> implements NamedArgumentFinder {
  44.     private final String                  prefix;
  45.     private final PMessageOrBuilder<M>    message;
  46.     private final Map<PField<M>, Integer> fieldTypes;

  47.     /**
  48.      * Create a named argument finder.
  49.      *
  50.      * @param prefix Optional prefix name. E.g. "x" will make for lookup
  51.      *               tags like ":x.my_field".
  52.      * @param message The message to look up fields in.
  53.      * @param fieldTypes Overriding of default field types. This can contain
  54.      *                   fields for any of the contained message types, and
  55.      *                   will be mapped whenever the field is selected.
  56.      */
  57.     public MessageNamedArgumentFinder(@Nullable String prefix,
  58.                                       @Nonnull PMessageOrBuilder<M> message,
  59.                                       @Nonnull Map<PField<M>, Integer> fieldTypes) {
  60.         this.message = message;
  61.         this.prefix = isNullOrEmpty(prefix) ? "" : prefix + ".";
  62.         this.fieldTypes = UnmodifiableMap.copyOf(fieldTypes);
  63.     }

  64.     @Override
  65.     public String toString() {
  66.         return prefix + "{" + message.descriptor().getQualifiedName() + "}";
  67.     }

  68.     @Override
  69.     @SuppressWarnings("unchecked")
  70.     public Argument find(String name) {
  71.         if (!prefix.isEmpty()) {
  72.             if (name.startsWith(prefix)) {
  73.                 name = name.substring(prefix.length());
  74.             } else {
  75.                 return null;
  76.             }
  77.         }

  78.         String[]             parts          = name.split("\\.", Byte.MAX_VALUE);
  79.         PMessageOrBuilder<?> leaf           = message;
  80.         PMessageDescriptor   leafDescriptor = message.descriptor();

  81.         for (int i = 0; i < parts.length - 1; ++i) {
  82.             String part = parts[i];
  83.             PField field = leafDescriptor.findFieldByName(part);
  84.             if (field == null) return null;
  85.             if (field.getType() != PType.MESSAGE) {
  86.                 throw new IllegalArgumentException("");
  87.             }
  88.             leafDescriptor = (PMessageDescriptor) field.getDescriptor();
  89.             if (leaf != null) {
  90.                 leaf = leaf.get(field.getId());
  91.             }
  92.         }
  93.         String leafName = parts[parts.length - 1];
  94.         PField field = leafDescriptor.findFieldByName(leafName);
  95.         if (field != null) {
  96.             if (leaf != null) {
  97.                 return new MessageFieldArgument(leaf, field, getColumnType(field));
  98.             }
  99.             return new NullArgument(getColumnType(field));
  100.         }
  101.         return null;
  102.     }

  103.     private int getColumnType(PField field) {
  104.         return fieldTypes.getOrDefault(field, MessageFieldArgument.getDefaultColumnType(field));
  105.     }

  106. }