Giter Site home page Giter Site logo

Nullable Types about machete HOT 7 CLOSED

phatboyg avatar phatboyg commented on May 25, 2024
Nullable Types

from machete.

Comments (7)

ahives avatar ahives commented on May 25, 2024

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.

nycbauer avatar nycbauer commented on May 25, 2024

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.

ahives avatar ahives commented on May 25, 2024

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.

nycbauer avatar nycbauer commented on May 25, 2024

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.

Program.txt

from machete.

ahives avatar ahives commented on May 25, 2024

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.

ahives avatar ahives commented on May 25, 2024

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.

ahives avatar ahives commented on May 25, 2024

@nycbauer is this still an issue?

from machete.

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.