scottmatthewman / swiftcsvencoder Goto Github PK
View Code? Open in Web Editor NEWA Swift package to allow easy encoding of data to CSV format
License: MIT License
A Swift package to allow easy encoding of data to CSV format
License: MIT License
Different applications that are capable of importing CSV files have different ways of handling some data types. Indeed, that it why CSVEncoderConfiguration
exists.
But each app generally wants the same format all the time, and developers shouldn’t need to have to remember (or investigate) which date formats are needed for the most popular apps.
We could provide static instances of CSVEncoderConfiguration
that have the necessary encoding strategies predefined, for example:
CSVEncoderConfiguration.excel // All the necessary config to ensure CSV files are readable by Microsoft Excel
CSVEncoderConfiguration.numbers // All the necessary config to ensure CSV files are readable by Apple Numbers
This would make the definitions more expressive while hiding the implementation detail:
let table = CSVTable<Person>(columns: [ … ], configuration: .excel)
Booleans are currently configured to output as the text strings true
or false
.
Add a new configuration parameter to CSVEncoderConfiguration
to support different styles of encoding:
encoding type | True encodes as | False encodes as |
---|---|---|
.trueFalse (default) |
true |
false |
.trueFalseUppercase |
TRUE |
FALSE |
.yesNo |
yes |
no |
.yesNoUppercase |
YES |
NO |
.integer |
1 |
0 |
Expected work to do:
BoolEncodingStrategy
enum to CSVEncoderConfiguration
with all options documentedboolEncodingStrategy
property to CSVEncoderConguration
CSVEncoderConfiguration.default
to use the default [^1]Bool.encode()
method to amend output based on configuration strategyCurrently, Double
and Int
have CSVEncodable
support added. Any other numeric types have to handle their own encoding within the CSVColumn
configuration.
Expanding the list of support to Float
, UInt
, etc., could be useful.
Include a privacy manifest file indicating that SwiftCSVEncoder does not collect any data.
Currently, CSV tables are defined as in the following example:
let table = CSVTable<Person>(
columns: [
CSVColumn("ID", \.id),
CSVColumn("Name", \.name),
// etc.
],
configuration: .default
)
This isn't great for situations where I might need a more dynamic approach to column inclusion. For example, only users with a certain level of access may have access to personally sensitive information and their CSVs shouldn't attempt to include those columns. Or, I may have a feature in the app that is governed by a feature flag, and columns should be omitted if that flag is not on.
Another approach would be to use a ResultBuilder-style syntax for building table definitions, say:
CSVTable(configuration: .default) {
Column("ID", \.id)
Column("Name", \.name)
if currentUser.isSuperAdmin {
Column("Address", \.postalAddress)
Column("Tel", \.telephone)
}
if FeatureSet.includes(.starRatings) {
Column("Star rating") { StarRating($0.value) }
}
}
This might also allow for configuration to be broken up and implemented as modifiers:
CSVTable {
Column("ID", \.id)
Column("Date of birth", \.date)
.dateFormat(.iso8601)
}
.fieldDelimiter(.tab)
Currently, the configuration object that governs encoding strategies for dates (and, when #2 is implemented, Booleans) is defined in the CSVTable
initialiser.
However, it is only used at the export stage. And it could be argued that it only matters there: the CSVTable
is a definition of the structure, and it is only when we solidify that data that we actually care what format to use. This could allow the same CSVTable
structure to be encoded in slightly different ways, for example, if Excel and/or Numbers needed slightly different formats than a custom app with its own CSV importer.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.