Comments (1)
You can use JSON for persistence too. One of the original reasons I made this was to store the same data in SQL and MongoDB, which uses JSON. The types in this package also implement TextMarshaler
which can be used for various storage packages.
Whether not you use the same structs for persistence and presentation is up to you. I don't prescribe any particular development philosophy here.
I embedded the sql.NullXYZ
structs in the types here so it can be a drop-in replacement for them. This also means I don't have to re-implement their Scan methods, and we can benefit from any standard library improvements in new Go versions. It also serves to limit the scope of this library, instead of supporting every imaginable nullable type, we limit support to database/sql/driver.Value
's types.
Personally I think the zero
packages types are the most useful part of the this package, because you can decode Go-friendly JSON data straight into nullable SQL types without having to manually convert them all yourself. For example, you could use it to store data from a JSON file in an SQL database.
Because encoding/json
's omitempty doesn't support non-pointer structs, this library is actually not great for presentation.
For greenfield development, I think it's best to eliminate nullable usage altogether and work with Go's zero values instead of against them. My original use-case was for interfacing with a legacy SQL database. You can also just use pointers.
I think eliminating the SQL dependency is completely fine if you want to focus on presentation, but until encoding/json
is improved, pointers will likely be the better choice. Feel free to fork and experiment.
Also, when generics come with Go 2.0, I might experiment with generic nullable types and remove the embedded SQL stuff (or maybe make a new package). Until then, this project's API is unlikely to change.
from null.
Related Issues (20)
- can add custom time format support for Time? HOT 1
- Int from string support?
- Bad marshaling implementation on null.Time struct
- Why does null.Time text-marshaling return null string? HOT 3
- Release v4 HOT 2
- Request parsing for null.Time HOT 1
- null.String composite literal uses unkeyed fields HOT 2
- error undefined: sql.NullTime HOT 1
- Storing null values using this library in database HOT 3
- Strings.StringFrom() passes Valid as true even if the string is empty HOT 3
- how versions are working HOT 1
- undefined: sql.NullTime HOT 1
- Bug on undefined type HOT 1
- [Question] Is it possible to have SQL null values treated as null in Go but encode them to zero in JSON? HOT 2
- Issue with omitempty HOT 4
- Not to use fmt.Errorf for UnmarshalJSON error HOT 6
- Generics support HOT 7
- how use json null set sql time field to null? HOT 3
- uint64 support HOT 2
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 null.