Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: bash -c 'echo "let appVersion = \"`git describe --abbrev=0 --tags` (`date +%F`)\""' > ./Sources/table/Version.swift
- name: Build Linux
if: ${{ matrix.os == 'ubuntu-latest' }}
run: swift build -c release
run: swift build -c release --static-swift-stdlib
- name: Rename file
if: ${{ matrix.os == 'ubuntu-latest' }}
run: bash -c 'mv .build/release/table ./table'
Expand Down
12 changes: 12 additions & 0 deletions Sources/table/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ class Header {
cols.firstIndex(of: ofColumn)
}

func filter(indexes: Set<Int>) -> Header {
let filteredCols = cols.enumerated().filter { index, col in
indexes.contains(index)
}.map { $0.element }

let filteredTypes = cols.enumerated().filter { index, col in
indexes.contains(index)
}.map { $0.offset < types.count ? types[$0.offset] : .string }

return Header(components: filteredCols, types: filteredTypes)
}

func type(ofColumn: String) -> CellType? {
guard let index = index(ofColumn: ofColumn) else { return .string }
return index < types.count ? types[index] : .string
Expand Down
9 changes: 9 additions & 0 deletions Sources/table/MainApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ struct MainApp: AsyncParsableCommand {
)
var addColumns: [String] = []

@Option(name: .customLong("remove"), help: "Removes specified columns from the output. Example: --remove password,token.")
var removeColumns: [String] = []

@Option(name: .customLong("distinct"), help: "Returns only distinct values for the specified column set. Example: --distinct name,city_id.")
var distinctColumns: [String] = []

Expand Down Expand Up @@ -248,6 +251,12 @@ struct MainApp: AsyncParsableCommand {
table = SampledTableView(table: table, percentage: sample)
}

if !removeColumns.isEmpty {
debug("Removing columns: \(removeColumns.joined(separator: ","))")
try removeColumns.forEach { if table.header.index(ofColumn: $0) == nil { throw RuntimeError("Column \($0) in remove clause is not found in the table") } }
table = HideColumnsTableView(table: table, hideColumns: removeColumns)
}

let printer = try buildPrinter(formatOpt: formatOpt, outFileFmt: try FileType.outFormat(strFormat: asFormat), outputFile: outputFile)

// when print format is set, header is not relevant anymore
Expand Down
46 changes: 40 additions & 6 deletions Sources/table/TableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ class JoinTableView: Table {
class NewColumnsTableView: Table {
var table: any Table
let additionalColumns: [(String, Format)]

var header: Header {
get {
return self.table.header + Header(components: additionalColumns.map { $0.0 }, types: additionalColumns.map { _ in CellType.string })
}
}
let header: Header

init(table: any Table, additionalColumns: [(String, Format)]) {
self.table = table
self.additionalColumns = additionalColumns
self.header = self.table.header +
Header(
components: additionalColumns.map { $0.0 },
types: additionalColumns.map { _ in CellType.string }
)
}

func next() throws -> Row? {
Expand All @@ -68,6 +68,40 @@ class NewColumnsTableView: Table {
}
}

/** Table view with additional dynamic columns */
class HideColumnsTableView: Table {
var table: any Table
let hideColumns: [String]
let hidenIndexes: [Int]
let header: Header

init(table: any Table, hideColumns: [String]) {
self.table = table
self.hideColumns = hideColumns
self.hidenIndexes = hideColumns.compactMap { col in
table.header.index(ofColumn: col)
}

self.header = table.header.filter(indexes: Set(0..<table.header.size).subtracting(hidenIndexes))
}

func next() throws -> Row? {
let row = try table.next()

if let row {
return Row(
header: header,
index: row.index,
cells: row.components.enumerated().filter { index, cell in
!hidenIndexes.contains(index)
}.map { $0.element }
)
} else {
return nil
}
}
}

/** Table view with filtered columns */
class ColumnsTableView: Table {
var table: any Table
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import XCTest
@testable import table

class NewColumnsTableViewTests: XCTestCase {
class ColumnsManipulationTableViewTests: XCTestCase {

func testAddSingleStaticColumn() throws {
let table = ParsedTable.fromArray([
Expand Down Expand Up @@ -330,5 +330,28 @@ class NewColumnsTableViewTests: XCTestCase {

XCTAssertNil(try newColumnsTable.next())
}

func testRemovingColumns() throws {
let table = ParsedTable.fromArray([
["Alice", "30", "Engineer"],
["Bob", "25", "Designer"]
], header: ["name", "age", "profession"])

let hideColumnsTable = HideColumnsTableView(
table: table,
hideColumns: ["age"]
)

XCTAssertEqual(hideColumnsTable.header.columnsStr(), "name,profession")
let row1 = try hideColumnsTable.next()!
XCTAssertEqual(row1["name"], "Alice")
XCTAssertEqual(row1["profession"], "Engineer")
XCTAssertNil(row1["age"])

let row2 = try hideColumnsTable.next()!
XCTAssertEqual(row2["name"], "Bob")
XCTAssertEqual(row2["profession"], "Designer")
XCTAssertNil(row2["age"])
}
}