Comments (4)
Hi Marcos,
That's extremely helpful—and now you've written it would make a great addition to your readme file on the repo! :)
I was previously looking for a point of customisation within CodableCSV—to define fields to pass over while writing the CSV file, without resorting to implementing your field-by-field write controls—rather than redefining the Codable process itself.
In this case, the data is previous webform submissions that are being accessed from a database, and other parts of the app rely on Codable as well, so redefining the encoding for the type is not an option. The third option you define would be possible, but now I understand this functionality isn't built natively into CodableCSV.
The application in question isn't dealing with large amounts of data at a time, so I think the most parsimonious approach is to simply map my existing objects to new structs that only contain the desired fields, and then pass those to Codable instead of the original model objects. Straightforward and doesn't risk side effects on Codable processing of these model objects in the future. Obviously that is incurring extra processing so would have some marginal performance cost but it's not going to be an issue for the current use case.
Thanks for your help. Most appreciated!
from codablecsv.
I am no sure I get what you mean. Could you provide a tiny example, please?
from codablecsv.
My object stores the result of a web form submission. It contains an ID property which is a UUID and the IP address of the submitter. I want to export the data in other properties as a CSV file, such as name, department, email so it can be downloaded by an admin user. However, they do not want the UUIDs in their file (clutter) and should not get the IP address.
Rather than having to create an intermediate object that does not contain these properties, I would like to be able to pass an array of my existing objects and just specify the properties of the object that should be left out of the CSV file.
from codablecsv.
Oh, I see. You have several ways to solve your problem. Let's suppose your web form submission is modeled as follows:
struct Submission: Encodable {
let id: UUID
let address: URL
let name: String
let department: String
let email: String
}
The id
and address
are to be ignored by a Codable encoder and name
, department
, and email
shall be encoded.
-
Option 1: Provide a
CodingKeys
enum only specifying the exact property names you want to encode.extension Submission { private enum CodingKeys: Int, CodingKey { case name = 0 case department = 1 case email = 2 } }
Then use the encoder as usual.
let submissions: [Submission] = [/* Array of submissions */] let encoder = CSVEncoder() let data = try encoder.encode(submissions, into: Data.self)
-
Option 2: Define the
encode(to:)
function instead of relying on Codable's code generation.extension Submission { func encode(to encoder: Encoder) throws { var container = encoder.unkeyedContainer() try container.encode(self.name) try container.encode(self.department) try container.encode(self.email) } }
Then use the encoder as usual.
-
Option 3: Define the
encode(to:)
function and conditionally encode properties depending on the existence ofuserInfo
keys.This is a bit more involved but let you have different encoding for different outputs. For example, you may want to encode non-technical properties for managers and technical properties for engineers.
Regards,
Marcos
from codablecsv.
Related Issues (20)
- Make it conform to TopLevelDecoder/Encoder to use with Combine HOT 2
- Encoding a single object produces a confusing error message. HOT 2
- Lazy decoding a CSV with CRLF line endings fails without an error HOT 4
- Extra comma in header or data line causes failure to parse subsequent lines HOT 8
- Decoding CSV file with CRLF line endings fail with error if the last column is quoted HOT 9
- trimStrategy does not trim characters inside of a quoted field HOT 2
- Value of type 'CSVReader' has no member 'columns'
- doesn't handle strings with "," HOT 1
- Option to escape Excel-unfriendly strings HOT 5
- How can i install on linux? HOT 1
- Add support for field delimiter detection HOT 1
- Give CSVWriter.Configuration its own delimiter type which has no inference API
- Skip Column in Encoder (and Decoder)
- Last column decodes as blank
- Last column decodes as blank HOT 1
- question about escaping
- Define headers, but suppress header output?
- Customise cell parsing for declarative decoder
- in iOS17 can't read file from CloudDocs
- Build error in Xcode v16.0 beta 5 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from codablecsv.