Coder Social home page Coder Social logo

Comments (7)

tonyd256 avatar tonyd256 commented on June 4, 2024

it should be as simple as just making it conform to that protocol. Make CurrencyValue conform to JSONDecodable and then implement the decode method for it.

from argo.

Blackjacx avatar Blackjacx commented on June 4, 2024

I have now this:

extension CurrencyValue : JSONDecodable {
    static func create(rawValue: String) -> CurrencyValue {
        if let newValue = CurrencyValue(rawValue: rawValue) {
            return newValue
        }
        return .unknown
    }

    static func decode(j: JSONValue) -> CurrencyValue? {
        return CurrencyValue.create
            <^> j <|  "amount"
            <*> j <|  "currency"
    }
}

But I fail somehow at the implementation of the decode method. I didn't get the Idea of that functional operators yet. Can you give me a tip?

from argo.

tonyd256 avatar tonyd256 commented on June 4, 2024

so your CurrencyValue type is an enum that represents a single string. To create an instance of the enum you have written the create function that takes a single string and parses it into a CurrencyValue instance. Now in the decode function you want to create that CurrencyValue with the string in the JSONValue using the create method.

without getting into what fmap and apply mean ... since your create function takes one parameter you should only give it one parameter ... in this example you are trying to pass in 2 parameters. Replace "amount" with "currency" and remove the second line.

from argo.

Blackjacx avatar Blackjacx commented on June 4, 2024

I tested this, but it didn't work. I now follow the idea to store whatever comes from the server as raw value and implement a parser function for every enum inside the model struct, to access the enum value... Maybe there is a more elegant solution to this.

The problem was probably that the JSONValue 'j' coming to decode() is already the value of the JSON field with the key currency, so there is nothing more to decode. I would just need to call the create function with the raw value. But then I think I circumvent the system and loose the error checking because I do not use the operators then... Am I at least a littl bit right :-D?

from argo.

tonyd256 avatar tonyd256 commented on June 4, 2024

If j is already the value you want you could just do return CurrencyValue.create <^> j.value() or ... <^> String.decode(j)

from argo.

Blackjacx avatar Blackjacx commented on June 4, 2024

Ahhh cool thank you very much 👍
That saves a lot of work!

from argo.

tonyd256 avatar tonyd256 commented on June 4, 2024

No problem. Closing this now.

from argo.

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.