Comments (7)
Nulls are not a good way to report that there is no data in a particular field. For this, we give you the HasValue property to test whether Value/ValueOrDefault will return a default or a value. So, you could have something like this:
if (!result.Select(x => x.SomeDateTimeField).HasValue)
// do something about it
from machete.
I don't agree with that, but I hear what you are saying.
However, since I am translating the data from a file to my own objects it's simpler to return a null for a value that is not there. This introduces an extra step.
Anyway, thank you for this library!
As an aside, it would be very helpful if there was some example code on reading a file. I had to navigate through the test classes to figure out how to use this library.
from machete.
Thanks for the reply.
Value is a monad and similar to what is called an Option type in functional programming. It is meant to either return a value or none value.
Tell me a little more about your use case. For example, are you using HL7 or X12? How big is the file you need to parse? Are you using LINQ to HL7/X12 or Layouts? Can you share code samples?
We have at least 4 different parsers, so I can perhaps guide you to the right one.
Would definitely be interested in understanding how are you using Machete so that we can make it better.
from machete.
Interesting. I've never really encountered a monad, however, I see how it's useful here.
I'm right now trying to parse an X12/835 document just for display purposes, but eventually I will be saving some of it to my database.
I am working with an MVC/WebApi application where I'm having my users upload a document to our site, which we then parse and either display some data and save other parts. So far, what I'm doing is to pass the document to your parser, and then go through the document and convert it to concrete class. in the form of this class it makes it easier for me map it to my database, and display it to the user in the form of json. In particular,
I've attached some code I've written to test out using your parser. I made some mistakes using it (for example, later on I realized how to go through the LayoutList, which I was surprised has no enumerator), and I'm sure there are others.
from machete.
Thanks for the response. There will be a couple of fixes in the next release based on Issue #55 and Issue #56.
Additionally, here are some usage recommendations:
Use Select liberally. It is what's called an Applicative Functor (or just Applicative) in Functional Programming and is used to unwrap the Value and return a safe T. So, instead of doing:
public PaymentInformation(Segment<BPR> s)
{
if (s.HasValue) {
this.HandleCode = s.Value.TransactionHandlingCode.ValueOrDefault();
this.Amount = s.Value.TotalActualProviderPaymentAmount.ValueOrDefault();
this.PaymentMethod = s.Value.PaymentMethodCode.ValueOrDefault();
this.PaymentEffectiveDate = s.Value.CheckIssueOrEftEffectiveDate.ValueOrDefault();
}
}
...you can rewrite this as:
public PaymentInformation(Segment<BPR> s)
{
if (s.HasValue) {
this.HandleCode = s.Select(x => x.TransactionHandlingCode).ValueOrDefault();
this.Amount = s.Select(x => x.TotalActualProviderPaymentAmount).ValueOrDefault();
this.PaymentMethod = s.Select(x => x.PaymentMethodCode).ValueOrDefault();
this.PaymentEffectiveDate = s.Select(x => x.CheckIssueOrEftEffectiveDate).ValueOrDefault();
}
}
Calling Value directly can cause an exception to be thrown because it attempts to strongly type the underlying string data per the what is defined in the Schema. If for example you want to return a DateTime and the source text has a "Hello World" where it is define to be a DateTime then an exception will be thrown. To avoid this use the Select applicative along with ValueOrDefault and the parser guarantees it will always return a result even if it is invalid but never an exception. This is how we enforce type safety.
So, instead of doing...
Entity eref;
l.Value.AdditionalIdentification.TryGetValue(0, out eref);
if (eref.HasValue)
{
this.Id = eref.Value.ReferenceIdentification.ValueOrEmpty();
}
...you could do something like...
if (!ll.TryGetValue(0, out Layout<L1000A_835> l) || !l.HasValue)
return;
var referenceIdentification = l.Select(x => x.AdditionalIdentification)[0].Select(x => x.ReferenceIdentification);
if (referenceIdentification.HasValue)
this.Id = referenceIdentification.ValueOrEmpty();
The fix for LayoutList and other List enumerators have been fixed and will be in next release. You will be able to do something like:
IEnumerable<Entity> notes = result.Select(x => x.Notes).ToEnumerable();
...or...
foreach (var note in result.Select(x => x.Notes).ToEnumerable())
from machete.
Hey, nycbauer please join our chatroom at https://gitter.im/PhatBoyG-Machete/Lobby?utm_source=share-link&utm_medium=link&utm_campaign=share-link
from machete.
@nycbauer is this still an issue?
from machete.
Related Issues (20)
- X12 - incorrect mapping HOT 13
- Add Apache 2.0 license on source code HOT 1
- Documentation and Project Page Update HOT 3
- Cannot stream parse through multiple layouts in the same transaction in X12 HOT 1
- Map condition doesn't work when SegmentList is followed by Segment of same entity type HOT 1
- Create a way to initialize entities through Translate API
- X12 Select applicative throwing NullReferenceException when segment missing HOT 2
- Accessing missing Layout through indexer off of Select applicative throwing ValueMissingException HOT 4
- Fix issues with HL7 2.6 schema HOT 1
- Create way to override entity registration
- DateTimePeriod in X12 Schema not implemented HOT 3
- Layout parser failing to parse X12 loop HOT 1
- Layout parser failing to parse loop 2300 when PAT segment missing
- Not able to add user-defined layouts when registering the schema
- Add ability to return all registered layouts and entities
- Need ability to format a Layout
- Really poor performance with streaming parser HOT 4
- Update 5010 AAA segment HOT 2
- 5010 CTT Segment - Description should be string HOT 2
- Support H3 Segment for 5010 spec HOT 1
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 machete.