GithubHelp home page GithubHelp logo

Comments (4)

babbage avatar babbage commented on September 13, 2024 1

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.

dehesa avatar dehesa commented on September 13, 2024

I am no sure I get what you mean. Could you provide a tiny example, please?

from codablecsv.

babbage avatar babbage commented on September 13, 2024

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.

dehesa avatar dehesa commented on September 13, 2024

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 of userInfo 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.