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

com.google.cloud.dataflow.sdk.util.Structs Maven / Gradle / Ivy

/*
 * Copyright (C) 2015 Google Inc.
 *
 * Licensed 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 com.google.cloud.dataflow.sdk.util;

import com.google.api.client.util.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

/**
 * A collection of static methods for manipulating datastructure representations
 * transferred via the Dataflow API.
 */
public final class Structs {
  private Structs() {}  // Non-instantiable

  public static String getString(Map map, String name) throws Exception {
    return getValue(map, name, String.class, "a string");
  }

  public static String getString(
      Map map, String name, @Nullable String defaultValue)
      throws Exception {
    return getValue(map, name, String.class, "a string", defaultValue);
  }

  public static byte[] getBytes(Map map, String name) throws Exception {
    @Nullable byte[] result = getBytes(map, name, null);
    if (result == null) {
      throw new ParameterNotFoundException(name, map);
    }
    return result;
  }

  @Nullable
  public static byte[] getBytes(Map map, String name, @Nullable byte[] defaultValue)
      throws Exception {
    @Nullable String jsonString = getString(map, name, null);
    if (jsonString == null) {
      return defaultValue;
    }
    // TODO: Need to agree on a format for encoding bytes in
    // a string that can be sent to the backend, over the cloud
    // map task work API.  base64 encoding seems pretty common.  Switch to it?
    return StringUtils.jsonStringToByteArray(jsonString);
  }

  public static Boolean getBoolean(Map map, String name) throws Exception {
    return getValue(map, name, Boolean.class, "a boolean");
  }

  @Nullable
  public static Boolean getBoolean(
      Map map, String name, @Nullable Boolean defaultValue)
      throws Exception {
    return getValue(map, name, Boolean.class, "a boolean", defaultValue);
  }

  public static Long getLong(Map map, String name) throws Exception {
    return getValue(map, name, Long.class, "an int");
  }

  @Nullable
  public static Long getLong(Map map, String name, @Nullable Long defaultValue)
      throws Exception {
    return getValue(map, name, Long.class, "an int", defaultValue);
  }

  @Nullable
  public static List getStrings(
      Map map, String name, @Nullable List defaultValue)
      throws Exception {
    @Nullable Object value = map.get(name);
    if (value == null) {
      if (map.containsKey(name)) {
        throw new IncorrectTypeException(name, map, "a string or a list");
      }
      return defaultValue;
    }
    if (Data.isNull(value)) {
      // This is a JSON literal null.  When represented as a list of strings,
      // this is an empty list.
      return Collections.emptyList();
    }
    @Nullable String singletonString = decodeValue(value, String.class);
    if (singletonString != null) {
      return Collections.singletonList(singletonString);
    }
    if (!(value instanceof List)) {
      throw new IncorrectTypeException(name, map, "a string or a list");
    }
    @SuppressWarnings("unchecked")
    List elements = (List) value;
    List result = new ArrayList<>(elements.size());
    for (Object o : elements) {
      @Nullable String s = decodeValue(o, String.class);
      if (s == null) {
        throw new IncorrectTypeException(name, map, "a list of strings");
      }
      result.add(s);
    }
    return result;
  }

  public static Map getObject(Map map, String name)
      throws Exception {
    @Nullable Map result = getObject(map, name, null);
    if (result == null) {
      throw new ParameterNotFoundException(name, map);
    }
    return result;
  }

  @Nullable
  public static Map getObject(
      Map map, String name, @Nullable Map defaultValue)
      throws Exception {
    @Nullable Object value = map.get(name);
    if (value == null) {
      if (map.containsKey(name)) {
        throw new IncorrectTypeException(name, map, "an object");
      }
      return defaultValue;
    }
    return checkObject(value, map, name);
  }

  private static Map checkObject(
      Object value, Map map, String name) throws Exception {
    if (Data.isNull(value)) {
      // This is a JSON literal null.  When represented as an object, this is an
      // empty map.
      return Collections.emptyMap();
    }
    if (!(value instanceof Map)) {
      throw new IncorrectTypeException(name, map, "an object (not a map)");
    }
    @SuppressWarnings("unchecked")
    Map mapValue = (Map) value;
    if (!mapValue.containsKey(PropertyNames.OBJECT_TYPE_NAME)) {
      throw new IncorrectTypeException(name, map,
          "an object (no \"" + PropertyNames.OBJECT_TYPE_NAME + "\" field)");
    }
    return mapValue;
  }

  @Nullable
  public static List> getListOfMaps(Map map, String name,
      @Nullable List> defaultValue) throws Exception {
    @Nullable
    Object value = map.get(name);
    if (value == null) {
      if (map.containsKey(name)) {
        throw new IncorrectTypeException(name, map, "a list");
      }
      return defaultValue;
    }
    if (Data.isNull(value)) {
      // This is a JSON literal null.  When represented as a list,
      // this is an empty list.
      return Collections.>emptyList();
    }

    if (!(value instanceof List)) {
      throw new IncorrectTypeException(name, map, "a list");
    }

    List elements = (List) value;
    for (Object elem : elements) {
      if (!(elem instanceof Map)) {
        throw new IncorrectTypeException(name, map, "a list of Map objects");
      }
    }

    @SuppressWarnings("unchecked")
    List> result = (List>) elements;
    return result;
  }

  public static Map getDictionary(
      Map map, String name) throws Exception {
    @Nullable Object value = map.get(name);
    if (value == null) {
      throw new ParameterNotFoundException(name, map);
    }
    if (Data.isNull(value)) {
      // This is a JSON literal null.  When represented as a dictionary, this is
      // an empty map.
      return Collections.emptyMap();
    }
    if (!(value instanceof Map)) {
      throw new IncorrectTypeException(name, map, "a dictionary");
    }
    @SuppressWarnings("unchecked")
    Map result = (Map) value;
    return result;
  }

  @Nullable
  public static Map getDictionary(
      Map map, String name, @Nullable Map defaultValue)
      throws Exception {
    @Nullable Object value = map.get(name);
    if (value == null) {
      if (map.containsKey(name)) {
        throw new IncorrectTypeException(name, map, "a dictionary");
      }
      return defaultValue;
    }
    if (Data.isNull(value)) {
      // This is a JSON literal null.  When represented as a dictionary, this is
      // an empty map.
      return Collections.emptyMap();
    }
    if (!(value instanceof Map)) {
      throw new IncorrectTypeException(name, map, "a dictionary");
    }
    @SuppressWarnings("unchecked")
    Map result = (Map) value;
    return result;
  }

  // Builder operations.

  public static void addString(Map map, String name, String value) {
    addObject(map, name, CloudObject.forString(value));
  }

  public static void addBoolean(Map map, String name, boolean value) {
    addObject(map, name, CloudObject.forBoolean(value));
  }

  public static void addLong(Map map, String name, long value) {
    addObject(map, name, CloudObject.forInteger(value));
  }

  public static void addObject(
      Map map, String name, Map value) {
    map.put(name, value);
  }

  public static void addNull(Map map, String name) {
    map.put(name, Data.nullOf(Object.class));
  }

  public static void addLongs(Map map, String name, long... longs) {
    List> elements = new ArrayList<>(longs.length);
    for (Long value : longs) {
      elements.add(CloudObject.forInteger(value));
    }
    map.put(name, elements);
  }

  public static void addList(
      Map map, String name, List> elements) {
    map.put(name, elements);
  }

  public static void addStringList(Map map, String name, List elements) {
    ArrayList objects = new ArrayList<>(elements.size());
    for (String element : elements) {
      objects.add(CloudObject.forString(element));
    }
    addList(map, name, objects);
  }

  public static > void addList(
      Map map, String name, T[] elements) {
    map.put(name, Arrays.asList(elements));
  }

  public static void addDictionary(
      Map map, String name, Map value) {
    map.put(name, value);
  }

  public static void addDouble(Map map, String name, Double value) {
    addObject(map, name, CloudObject.forFloat(value));
  }

  // Helper methods for a few of the accessor methods.

  private static  T getValue(Map map, String name, Class clazz, String type)
      throws Exception {
    @Nullable T result = getValue(map, name, clazz, type, null);
    if (result == null) {
      throw new ParameterNotFoundException(name, map);
    }
    return result;
  }

  @Nullable
  private static  T getValue(
      Map map, String name, Class clazz, String type, @Nullable T defaultValue)
      throws Exception {
    @Nullable Object value = map.get(name);
    if (value == null) {
      if (map.containsKey(name)) {
        throw new IncorrectTypeException(name, map, type);
      }
      return defaultValue;
    }
    T result = decodeValue(value, clazz);
    if (result == null) {
      // The value exists, but can't be decoded.
      throw new IncorrectTypeException(name, map, type);
    }
    return result;
  }

  @Nullable
  private static  T decodeValue(Object value, Class clazz) {
    try {
      if (value.getClass() == clazz) {
        // decodeValue() is only called for final classes; if the class matches,
        // it's safe to just return the value, and if it doesn't match, decoding
        // is needed.
        return clazz.cast(value);
      }
      if (!(value instanceof Map)) {
        return null;
      }
      @SuppressWarnings("unchecked")
      Map map = (Map) value;
      @Nullable String typeName = (String) map.get(PropertyNames.OBJECT_TYPE_NAME);
      if (typeName == null) {
        return null;
      }
      @Nullable CloudKnownType knownType = CloudKnownType.forUri(typeName);
      if (knownType == null) {
        return null;
      }
      @Nullable Object scalar = map.get(PropertyNames.SCALAR_FIELD_NAME);
      if (scalar == null) {
        return null;
      }
      return knownType.parse(scalar, clazz);
    } catch (ClassCastException e) {
      // If any class cast fails during decoding, the value's not decodable.
      return null;
    }
  }

  private static final class ParameterNotFoundException extends Exception {
    private static final long serialVersionUID = 0;

    public ParameterNotFoundException(String name, Map map) {
      super("didn't find required parameter " + name + " in " + map);
    }
  }

  private static final class IncorrectTypeException extends Exception {
    private static final long serialVersionUID = 0;

    public IncorrectTypeException(String name, Map map, String type) {
      super("required parameter " + name + " in " + map + " not " + type);
    }
  }
}