<commit_before><?php
namespace Aliyun\OTS\Handlers;

use Aliyun\OTS;

use CreateTableRequest;
use DeleteTableRequest;
use DescribeTableRequest;
use UpdateTableRequest;
use GetRowRequest;
use PutRowRequest;
use UpdateRowRequest;
use DeleteRowRequest;
use BatchGetRowRequest;
use BatchWriteRowRequest;
use GetRangeRequest;

use ColumnType, OperationType, Condition, Column, ColumnValue, ColumnUpdate;
use Direction, ReservedThroughput, CapacityUnit;
use TableInBatchGetRowRequest, RowInBatchGetRowRequest;
use TableInBatchWriteRowRequest;
use PutRowInBatchWriteRowRequest;
use UpdateRowInBatchWriteRowRequest;
use DeleteRowInBatchWriteRowRequest;
use LogicalOperatorConst;
use ComparatorTypeConst;

class ProtoBufferEncoder
{
    private function checkParameter($request)
    {
        // TODO implement
    }

    private function preprocessColumnType($type)
    {
        switch ($type) {
            case 'INTEGER': return ColumnType::INTEGER;
            case 'STRING': return ColumnType::STRING;
            case 'BOOLEAN': return ColumnType::BOOLEAN;
            case 'DOUBLE': return ColumnType::DOUBLE;
            case 'BINARY': return ColumnType::BINARY;
            case 'INF_MIN': return ColumnType::INF_MIN;
            case 'INF_MAX': return ColumnType::INF_MAX;
            default:
                throw new \Aliyun\OTS\OTSClientException("Column type must be one of 'INTEGER', 'STRING', 'BOOLEAN', 'DOUBLE', 'BINARY', 'INF_MIN', or 'INF_MAX'.");
        }
    }

    private function preprocessColumnValue($columnValue)
    {
        if (is_bool($columnValue)) {

            // is_bool() is checked before is_int(), to avoid type upcasting
            $columnValue = array('type' => 'BOOLEAN', 'value' => $columnValue);

        } else if (is_int($columnValue)) {
            $columnValue = array('type' => 'INTEGER', 'value' => $columnValue);
        } else if (is_string($columnValue)) {
            $columnValue = array('type' => 'STRING', 'value' => $columnValue);
        } else if (is_double($columnValue) || is_float($columnValue)) {
            $columnValue = array('type' => 'DOUBLE', 'value' => $columnValue);
        } else if (is_array($columnValue)) {
            if (!isset($columnValue['type'])) {
                throw new \Aliyun\OTS\OTSClientException("An array column value must has 'type' field.");
            }

            if ($columnValue['type'] != 'INF_MIN' && $columnValue['type'] != 'INF_MAX' && !isset($columnValue['value'])) {
                throw new \Aliyun\OTS\OTSClientException("A column value wth type INTEGER, STRING, BOOLEAN, DOUBLE, or BINARY must has 'value' field.");
            }
        } else {
            throw new \Aliyun\OTS\OTSClientException("A column value must be a int, string, bool, double, float, or array.");
        }

        $type = $this->preprocessColumnType($columnValue['type']);
        $ret = array('type' => $type);

        switch ($type) {
            case ColumnType::INTEGER: 
                $ret['v_int'] = $columnValue['value'];
                break;
            case ColumnType::STRING: 
                $ret['v_string'] = $columnValue['value'];
                break;
            case ColumnType::BOOLEAN: 
                $ret['v_bool'] = $columnValue['value'];
                break;
            case ColumnType::DOUBLE:
                $ret['v_double'] = $columnValue['value'];
                break;
            case ColumnType::BINARY: 
                $ret['v_binary'] = $columnValue['value'];
                break;
            case ColumnType::INF_MIN:
                break;
            case ColumnType::INF_MAX:
                break;
        }

        return $ret;
    }

    private function preprocessColumns($columns)
    {
        $ret = array();

        foreach ($columns as $name => $value)
        {
            $data = array(
                'name' => $name,
                'value' => $this->preprocessColumnValue($value),
            );
            array_push($ret, $data);
        }

        return $ret;
    }
    
    private function preprocessLogicalOperator($logical_operator) 
    {   
    	if ( !is_int($logical_operator) || 
    	        ( $logical_operator != LogicalOperatorConst::AND && $logical_operator != LogicalOperatorConst::OR && $logical_operator != LogicalOperatorConst::NOT ) )
    	    throw new \Aliyun\OTS\OTSClientException("LogicalOperator must be one of 'LogicalOperatorConst::AND', 'LogicalOperatorConst::OR' or 'LogicalOperatorConst::NOT'.");
    	
    	return $logical_operator;
    }
    
    private function preprocessComparatorType($comparator_type) 
    {
    	if ( !is_int($comparator_type) || 
    	        ( $comparator_type != ComparatorTypeConst::EQUAL && 
    	                $comparator_type != ComparatorTypeConst::NOT_EQUAL && 
    	                $comparator_type != ComparatorTypeConst::GREATER_THAN && 
    	                $comparator_type != ComparatorTypeConst::GREATER_EQUAL &&
    	                $comparator_type != ComparatorTypeConst::LESS_THAN &&
    	                $comparator_type != ComparatorTypeConst::LESS_EQUAL ) )
    	    throw new \Aliyun\OTS\OTSClientException("Comparator must be one of 'ComparatorTypeConst::EQUAL', 'ComparatorTypeConst::NOT_EQUAL', 'ComparatorTypeConst::LESS_THAN', 'ComparatorTypeConst::LESS_EQUAL', 'ComparatorTypeConst::GREATER_THAN' or 'ComparatorTypeConst::GREATER_EQUAL'.");
 
    	return $comparator_type;
    }
    
    private function preprocessRowExistence($condition)
    {
    	$value=null;
    	if ( strcmp($condition, "IGNORE") == 0 )
    		$value = \RowExistenceExpectation::IGNORE;
    	else if ( strcmp($condition, "EXPECT_EXIST") == 0 )
    		$value = \RowExistenceExpectation::EXPECT_EXIST;
    	else if ( strcmp($condition, "EXPECT_NOT_EXIST") == 0 )
    		$value = \RowExistenceExpectation::EXPECT_NOT_EXIST;
    	else {
    		throw new \Aliyun\OTS\OTSClientException("Condition must be one of 'IGNORE', 'EXPECT_EXIST' or 'EXPECT_NOT_EXIST'.");
    	}
    	return $value;
    }
    
    private function preprocessColumnCondition($column_filters)
    {
    	$ret = array();
        	
    	foreach ($column_filters as $name => $value)
    	{
    		if ( strcmp( $name, "logical_operator" ) == 0 || strcmp( $name, "sub_conditions" ) == 0 ) {
    			// a composite condition
    			if ( strcmp( $name, "logical_operator" ) == 0 ) {
    				$value = $this->preprocessLogicalOperator($value);
    				$ret = array_merge( $ret, array( $name => $value ) );
    			} else if ( strcmp( $name, "sub_conditions" ) == 0 ) {
    				$sub_conditions = array();<commit_msg>replace string value with const<commit_after><?php
namespace Aliyun\OTS\Handlers;

use Aliyun\OTS;

use CreateTableRequest;
use DeleteTableRequest;
use DescribeTableRequest;
use UpdateTableRequest;
use GetRowRequest;
use PutRowRequest;
use UpdateRowRequest;
use DeleteRowRequest;
use BatchGetRowRequest;
use BatchWriteRowRequest;
use GetRangeRequest;

use ColumnType, OperationType, Condition, Column, ColumnValue, ColumnUpdate;
use Direction, ReservedThroughput, CapacityUnit;
use TableInBatchGetRowRequest, RowInBatchGetRowRequest;
use TableInBatchWriteRowRequest;
use PutRowInBatchWriteRowRequest;
use UpdateRowInBatchWriteRowRequest;
use DeleteRowInBatchWriteRowRequest;
use LogicalOperatorConst;
use ComparatorTypeConst;
use RowExistenceExpectationConst;

class ProtoBufferEncoder
{
    private function checkParameter($request)
    {
        // TODO implement
    }

    private function preprocessColumnType($type)
    {
        switch ($type) {
            case 'INTEGER': return ColumnType::INTEGER;
            case 'STRING': return ColumnType::STRING;
            case 'BOOLEAN': return ColumnType::BOOLEAN;
            case 'DOUBLE': return ColumnType::DOUBLE;
            case 'BINARY': return ColumnType::BINARY;
            case 'INF_MIN': return ColumnType::INF_MIN;
            case 'INF_MAX': return ColumnType::INF_MAX;
            default:
                throw new \Aliyun\OTS\OTSClientException("Column type must be one of 'INTEGER', 'STRING', 'BOOLEAN', 'DOUBLE', 'BINARY', 'INF_MIN', or 'INF_MAX'.");
        }
    }

    private function preprocessColumnValue($columnValue)
    {
        if (is_bool($columnValue)) {

            // is_bool() is checked before is_int(), to avoid type upcasting
            $columnValue = array('type' => 'BOOLEAN', 'value' => $columnValue);

        } else if (is_int($columnValue)) {
            $columnValue = array('type' => 'INTEGER', 'value' => $columnValue);
        } else if (is_string($columnValue)) {
            $columnValue = array('type' => 'STRING', 'value' => $columnValue);
        } else if (is_double($columnValue) || is_float($columnValue)) {
            $columnValue = array('type' => 'DOUBLE', 'value' => $columnValue);
        } else if (is_array($columnValue)) {
            if (!isset($columnValue['type'])) {
                throw new \Aliyun\OTS\OTSClientException("An array column value must has 'type' field.");
            }

            if ($columnValue['type'] != 'INF_MIN' && $columnValue['type'] != 'INF_MAX' && !isset($columnValue['value'])) {
                throw new \Aliyun\OTS\OTSClientException("A column value wth type INTEGER, STRING, BOOLEAN, DOUBLE, or BINARY must has 'value' field.");
            }
        } else {
            throw new \Aliyun\OTS\OTSClientException("A column value must be a int, string, bool, double, float, or array.");
        }

        $type = $this->preprocessColumnType($columnValue['type']);
        $ret = array('type' => $type);

        switch ($type) {
            case ColumnType::INTEGER: 
                $ret['v_int'] = $columnValue['value'];
                break;
            case ColumnType::STRING: 
                $ret['v_string'] = $columnValue['value'];
                break;
            case ColumnType::BOOLEAN: 
                $ret['v_bool'] = $columnValue['value'];
                break;
            case ColumnType::DOUBLE:
                $ret['v_double'] = $columnValue['value'];
                break;
            case ColumnType::BINARY: 
                $ret['v_binary'] = $columnValue['value'];
                break;
            case ColumnType::INF_MIN:
                break;
            case ColumnType::INF_MAX:
                break;
        }

        return $ret;
    }

    private function preprocessColumns($columns)
    {
        $ret = array();

        foreach ($columns as $name => $value)
        {
            $data = array(
                'name' => $name,
                'value' => $this->preprocessColumnValue($value),
            );
            array_push($ret, $data);
        }

        return $ret;
    }
    
    private function preprocessLogicalOperator($logical_operator) 
    {   
    	if ( !is_int($logical_operator) || 
    	        ( $logical_operator != LogicalOperatorConst::AND && $logical_operator != LogicalOperatorConst::OR && $logical_operator != LogicalOperatorConst::NOT ) )
    	    throw new \Aliyun\OTS\OTSClientException("LogicalOperator must be one of 'LogicalOperatorConst::AND', 'LogicalOperatorConst::OR' or 'LogicalOperatorConst::NOT'.");
    	
    	return $logical_operator;
    }
    
    private function preprocessComparatorType($comparator_type) 
    {
    	if ( !is_int($comparator_type) || 
    	        ( $comparator_type != ComparatorTypeConst::EQUAL && 
    	                $comparator_type != ComparatorTypeConst::NOT_EQUAL && 
    	                $comparator_type != ComparatorTypeConst::GREATER_THAN && 
    	                $comparator_type != ComparatorTypeConst::GREATER_EQUAL &&
    	                $comparator_type != ComparatorTypeConst::LESS_THAN &&
    	                $comparator_type != ComparatorTypeConst::LESS_EQUAL ) )
    	    throw new \Aliyun\OTS\OTSClientException("Comparator must be one of 'ComparatorTypeConst::EQUAL', 'ComparatorTypeConst::NOT_EQUAL', 'ComparatorTypeConst::LESS_THAN', 'ComparatorTypeConst::LESS_EQUAL', 'ComparatorTypeConst::GREATER_THAN' or 'ComparatorTypeConst::GREATER_EQUAL'.");
 
    	return $comparator_type;
    }
    
    private function preprocessRowExistence($condition)
    {
    	$value=null;
    	if ( strcmp($condition, RowExistenceExpectationConst::IGNORE) == 0 )
    		$value = \RowExistenceExpectation::IGNORE;
    	else if ( strcmp($condition, RowExistenceExpectationConst::EXPECT_EXIST) == 0 )
    		$value = \RowExistenceExpectation::EXPECT_EXIST;
    	else if ( strcmp($condition, RowExistenceExpectationConst::EXPECT_NOT_EXIST) == 0 )
    		$value = \RowExistenceExpectation::EXPECT_NOT_EXIST;
    	else {
    		throw new \Aliyun\OTS\OTSClientException("Condition must be one of 'RowExistenceExpectationConst::IGNORE', 'RowExistenceExpectationConst::EXPECT_EXIST' or 'RowExistenceExpectationConst::EXPECT_NOT_EXIST'.");
    	}
    	return $value;
    }
    
    private function preprocessColumnCondition($column_filters)
    {
    	$ret = array();
        	
    	foreach ($column_filters as $name => $value)
    	{
    		if ( strcmp( $name, "logical_operator" ) == 0 || strcmp( $name, "sub_conditions" ) == 0 ) {
    			// a composite condition
    			if ( strcmp( $name, "logical_operator" ) == 0 ) {
    				$value = $this->preprocessLogicalOperator($value);
    				$ret = array_merge( $ret, array( $name => $value ) );
    			} else if ( strcmp( $name, "sub_conditions" ) == 0 ) {
    				$sub_conditions = array();
{'fraction_non_alphanumeric': 0.1230413371138636, 'fraction_numerical': 0.001044620205939412, 'mean_word_length': 2.697379310344828, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2258551', 'n_tokens_mistral': 4039, 'n_tokens_neox': 3936, 'n_words': 817}