com.intuit.karate.core.Table Maven / Gradle / Ivy
The newest version!
/*
* The MIT License
*
* Copyright 2022 Karate Labs Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.intuit.karate.core;
import com.intuit.karate.StringUtils;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.graal.JsEngine;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author pthomas3
*/
public class Table {
private static final Logger logger = LoggerFactory.getLogger(Table.class);
enum ColumnType {
STRING,
EVALUATED
}
class Column {
final String key;
final int index;
final ColumnType type;
Column(String key, int index, ColumnType type) {
this.key = key;
this.index = index;
this.type = type;
}
}
private final List> rows;
private final Map colMap;
private final List cols;
private final List lineNumbers;
private final String dynamicExpression;
public String getDynamicExpression() {
return dynamicExpression;
}
public boolean isDynamic() {
return dynamicExpression != null;
}
public Collection getKeys() {
return colMap.keySet();
}
public int getLineNumberForRow(int i) {
return lineNumbers.get(i);
}
public Table replace(String token, String value) {
int rowCount = rows.size();
List keys = rows.get(0);
int colCount = keys.size();
List> list = new ArrayList(rowCount);
list.add(keys); // header row
for (int i = 1; i < rowCount; i++) { // don't include header row
List row = rows.get(i);
List replaced = new ArrayList(colCount);
list.add(replaced);
for (int j = 0; j < colCount; j++) {
String text = row.get(j).replace(token, value);
replaced.add(text);
}
}
return new Table(list, lineNumbers);
}
public String getValueAsString(String key, int row) {
Column col = colMap.get(key);
if (col == null) {
return null;
}
return rows.get(row).get(col.index);
}
public static Table fromKarateJson(List