flight-school / money Goto Github PK
View Code? Open in Web Editor NEWA precise, type-safe representation of a monetary amount in a given currency
Home Page: https://flight.school/books/numbers
License: MIT License
A precise, type-safe representation of a monetary amount in a given currency
Home Page: https://flight.school/books/numbers
License: MIT License
It would be nice to use this from CocoaPods! Hereβs a rough draft of a Podspec
that seems to work, albeit with a warning that the summary and description are identical:
Pod::Spec.new do |s|
s.name = "Money"
s.version = "1.0.0"
s.summary = "A precise, type-safe representation of a monetary amount in a given currency."
s.description = <<-DESC
A precise, type-safe representation of a monetary amount in a given currency.
DESC
s.homepage = "https://gumroad.com/l/swift-numbers"
s.license = { :type => "MIT", :file => "LICENSE.md" }
s.author = "Mattt"
s.social_media_url = "http://twitter.com/mattt"
s.swift_version = '4.0'
s.ios.deployment_target = "8.0"
s.osx.deployment_target = "10.10"
s.watchos.deployment_target = "2.0"
s.tvos.deployment_target = "9.0"
s.source = { :git => "https://github.com/Flight-School/Money.git", :tag => "#{s.version}" }
s.source_files = "Sources/Money/*.swift"
end
Hi,
I'm not sure if this is the best place to ask this question so bear with me in case this is more of a feature request or just not understanding the library clearly.
Some Background
I am building an app and all currency values are stored and passed in API's using Minor Values (e.g. cents or pence as needed by the currency). I'm using similar libraries for JavaScript and the Backend to be able to manage this.
I was looking for a similar library to understand and use this on Swift.
Usage
I have two steps in what I'm trying to do.
(1) Creating a Money value based on a currencyCode which is passed as a string.
I sense this is something I'm not fully understanding in Swift, but how do I instantiate the Money with the "USD" actually being a string?
(2) Creating the value from a Minor Value
I considered creating this as an extension of your class, which would need to do the following:
Again, apologies if there is something I'm not understanding, but any help or guidance would be much appreciated.
Thanks
Moe
Sure, it may seem like a big one, so if you agree that this ticket would be nice to have, I'll contribute.
Money's types, like CurrencyType
can't be collected in Sets or used in sophisticated cases in general since protocols can't be fancily extended on the dependent's end.
Specifically this issue tries to raise awareness on the necessity of Hashability.
Make things Hashable
that make sense. Like CurrencyType
.
Please make the "currencyCode" JSON key configurable to "currency", or please change this key to "currency" for better compatibility between libraries.
The JSON serialization/deserialization of a Money object does not seem to be configurable to another key besides "currencyCode". Whereas, "currency" seems to be the preferred default JSON key for money libraries in other languages (i.e. jackson-datatype-money & dinero). Also, not every library can be configured to use "currencyCode" as the JSON key, and "currency" is a shorter key that offers minor savings in bandwidth and storage costs.
You have to include it as
git "https://github.com/Flight-School/Money.git" "master"
to build.
Seems Swift Standard Library call all this name collisioned protocol XXXProtocol
, like IteratorProtocol
.
This is a fantastic piece of work by the way @mattt and has saved us from having to roll our own solution.
I did discover what could be a potential rounding issue when decoding Decimal's from json. I'm seeing the following test fail
Our solution to this was to perform the same rounding correction you already perform elsewhere in the code and add it to the decode function.
E.g.
self.amount = Money<Currency>(amount).rounded.amount
So the decode method becomes
Just thought I'd mention this incase I'm doing something dumb or it isn't dumb and could be useful to someone else.
I have one other question. We'd like to override the behaviour of the encode and decode functions to support decoding from a string and encoding to a single value and not the amount + currency (currently we're only using a single currency, but that could change down the line)
How can I extend or override these methods without forking the repo or plain copying your class? It seems as though you can't inherit from a generic class and override in the usual way.
Anyway thanks again for making this π
Hi,
like the effort here, thx!
One issue that comes up immediately is localization. Is there an easy way to use the currency defined from the current locale?
I have a project that includes Money version 1.2.0. The project has 4 targets, 1 run and 3 test targets. All 4 targets have set the Swift Language Version to Swift 5.
I recently started updating CocoaPods library dependencies. I got a build error after updating an unrelated CocoaPods library saying we couldn't add/subtract Money values. Turns out that when I had run pod update <CocoaPods Library Name>
, the updated pods project changed the SWIFT_VERSION from 5.0 to 4.2. If I reset the pods project SWIFT_VERSION back to 5.0, the project compiles without errors.
CocoaPods 1.7.0 adds support for multiple Swift versions in the Podspec. It also allows the Podfile to specify a swift version for a pod, using supports_swift_versions '>= 5.0'
. We're currently using CocoaPods 1.9.1, so I added that to our podfile and ran pod update. CocoaPods reported the following error:
Money-FlightSchool
does not specify a Swift version (4.2
) that is satisfied by any of targets (<AppTargetName>
) integrating it.
It doesn't appear there are any code changes needed to run Money on Swift 5. Can you update the podspec to allow running on the latest Swift versions?
Thank you for considering this!!
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.