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

io.warp10.ext.thrift.TOTHRIFT Maven / Gradle / Ivy

There is a newer version: 1.1.1-uberjar
Show newest version
//
//   Copyright 2020  SenX S.A.S.
//
//   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 io.warp10.ext.thrift;

import java.util.Map;

import org.apache.thrift.TSerializer;
import org.apache.thrift.protocol.TCompactProtocol;

import io.warp10.script.NamedWarpScriptFunction;
import io.warp10.script.WarpScriptException;
import io.warp10.script.WarpScriptStack;
import io.warp10.script.WarpScriptStackFunction;

public class TOTHRIFT extends NamedWarpScriptFunction implements WarpScriptStackFunction {
  
  public TOTHRIFT(String name) {
    super(name);
  }
  
  @Override
  public Object apply(WarpScriptStack stack) throws WarpScriptException {
    
    Object top = stack.pop();
    
    if (!(top instanceof DynamicTBase) && !(top instanceof String)) {
      throw new WarpScriptException(getName() + " expects a compiled Thrift structure (" + ThriftWarpScriptExtension.TBASE + ") or the name of one.");
    }

    if (top instanceof String) {
      String name = (String) top;
      top = stack.pop();
      
      if (!(top instanceof Map)) {
        throw new WarpScriptException(getName() + " expects a map of names to compiled Thrift structures (" + ThriftWarpScriptExtension.TBASE + ").");
      }
      
      top = ((Map) top).get(name);
      if (!(top instanceof DynamicTBase)) {
        throw new WarpScriptException(getName() + " expected '" + name + "' to be associated with a compiled Thrift structure (" + ThriftWarpScriptExtension.TBASE + ").");
      }
    }
    
    DynamicTBase tbase = ((DynamicTBase) top).clone();
    
    top = stack.pop();
    
    if (!(top instanceof Map)) {
      throw new WarpScriptException(getName() + " operates on a MAP.");
    }
    
    Map map = (Map) top;
    
    // Set the structure of tbase to be map
    tbase.setStruct(map);
    
    try {
      TSerializer serializer = new TSerializer(new TCompactProtocol.Factory());
      byte[] bytes = serializer.serialize(tbase);
      stack.push(bytes);      
    } catch (Exception e) {
      throw new WarpScriptException(getName() + " encountered an error during serialization attempt.", e);
    }
    
    return stack;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy