cake icon indicating copy to clipboard operation
cake copied to clipboard

Add helper for argument validation

Open pascalberger opened this issue 7 years ago • 55 comments

Each Cake addin requires some kind of argument validation (or at least should :). Instead of having to implement this in every addin it would be nice if this could be provided for common cases (null check, empty string check, ...) by Cake.Core.

pascalberger avatar Apr 03 '17 09:04 pascalberger

This could also be helpful Cake internally to simplify argument checking, like its eg done here.

If this is of interest I can provide a PR for a class similar to the one which we use in the Cake.Prca Addin.

pascalberger avatar Apr 03 '17 09:04 pascalberger

@pascalberger I would have no objection to this.

@cake-build/cake-team thoughts?

gep13 avatar Apr 24 '17 21:04 gep13

This sounds pretty awesome if it makes it easier. It is a common pattern in every addin.

phillipsj avatar Apr 25 '17 01:04 phillipsj

I agree, this would be great. There's already several argument validation frameworks - I wonder if any of them have source-only NuGet packages...

daveaglick avatar Apr 25 '17 02:04 daveaglick

Agreed that it would be good to centralise this (few other candidates for that) 👍

@daveaglick I've used Ensure.That from @danielwertheim before, but looks like the source-only version hasn't gotten the latest updates yet..

agc93 avatar Apr 25 '17 03:04 agc93

Agreed!

But instead of signature void Check<T>(T...) I would rather see T Check<T>(T...). Then you could write checks like

_foo = Check.NotNull(foo);

I personally think ASP.NET team have a quite decent boilerplate here which I have used from time to time.

bjorkstromm avatar Apr 25 '17 04:04 bjorkstromm

Additional reason to centralise this I just noticed: the Cake.Prca validation examples @pascalberger linked to currently show up on just about every type in the documentation, which could be a bit misleading, even with the From PrcaArgumentChecks)

agc93 avatar Apr 25 '17 23:04 agc93

I like the link @mholo65 posted.

patriksvensson avatar Apr 26 '17 13:04 patriksvensson

OK, I'll create a PR containing a class based on the ASP.NET boilerplate.

pascalberger avatar Apr 26 '17 14:04 pascalberger

@pascalberger Just make sure to attribute ASP.NET project in your code.

patriksvensson avatar Apr 26 '17 14:04 patriksvensson

Since there's currently only interest for aliases providing argument checking but not also for a public class which can be consumed in addins, I'll leave this to someone else for implementing (or there is also already an implementation for such aliases in Cake.Incubator).

pascalberger avatar May 01 '17 06:05 pascalberger

Adding Up-for-grabs on this one.

This is what we thought of:

  • Add internal static class for argument checking. You could use this and attribute ASP.NET team in the comments. This file should be sourced in every project (<Compile Include="..\Shared\Check.cs" />)
  • Change argument checking to use new approach. E.g. change this:
if (foo == null)
{
    throw new ArgumentNullException(nameof(foo));
}
_foo = foo;

to:

_foo = Check.NotNull(nameof(foo));

bjorkstromm avatar May 01 '17 20:05 bjorkstromm

@mholo65 Perhaps we should reserve this one for https://yourfirstpr.github.io/? I could do the mentoring.

patriksvensson avatar May 02 '17 07:05 patriksvensson

@patriksvensson sounds good to me!

bjorkstromm avatar May 02 '17 08:05 bjorkstromm

@patriksvensson I'd be interested in trying this out

bookman220 avatar Jun 15 '17 23:06 bookman220

@bookman220 Great! Do you want any help to get started or do you want to give it a shot yourself?

patriksvensson avatar Jun 16 '17 08:06 patriksvensson

@patriksvensson I'll probably need some help but let me figure out how to build this thing first.

bookman220 avatar Jun 17 '17 03:06 bookman220

@patriksvensson @bookman220 I don't want to step on anyone's toes, I'm interested in trying this out if it is still up for grabs. This would be my first time contributing to open source.

sarithsutha avatar Aug 11 '17 15:08 sarithsutha

@sarithsutha @patriksvensson I do apologize for taking so long. I do think I may need some guidance however. Do I just open up the solution file in visual studio and compile it from there? If I haven't fixed the big within the next week you may take it sarithsutha

bookman220 avatar Aug 11 '17 20:08 bookman220

@bookman220 thanks for your response. No rush, take your time, I don't mind waiting. I use Visual Studio 2017, I was able to just open the solution and compile it from there. And with VS code I was able to build using the Cake addin.

sarithsutha avatar Aug 11 '17 20:08 sarithsutha

@sarithsutha It appears I am having some trouble building the project. Apologies for needing such basic advice but I am new to Visual Studio and C#. It appears a fair amount of the solution failed to load and when I go to Build it just has the option to run a code analysis on the solution, which I did and you can see the output. Can you give me some pointers as to where to go from here? image

bookman220 avatar Aug 19 '17 19:08 bookman220

@bookman220 There seems to be problems loading the different projects according to the screenshot.

The following prerequisites need to be installed:

  • Visual Studio 2017 (https://www.visualstudio.com/vs/)
  • .NET Core SDK 1.0.4 or later (https://www.microsoft.com/net/download/core)

The build.cake file is only used to build the project from command line. When the project is loading correctly in Visual Studio, you should be able to build everything from the Build menu.

patriksvensson avatar Aug 20 '17 06:08 patriksvensson

@patriksvensson I'd like to take a try on this one.

micheleissa avatar Oct 26 '17 18:10 micheleissa

What I'm planning to do :

  • Add folder called 'Shared' in the solution directory.
  • Add 'Check.cs' inside that folder.
  • Modify each csproj file to include
<ItemGroup>
    <Compile Include="..\Shared\Check.cs" />
</ItemGroup>
  • The namespace for it Cake.Shared
  • I saw the link from ASP.NET team but I feel it is more complicated than it should be and I was thinking of something along the following
public static T NotNull<T>(T o, string message = null)
            {
            if (o == null)
                {
                throw new ArgumentNullException(message ?? $"{nameof(o)} cannot be null");
                }
            return o;
            }

Thoughts?

micheleissa avatar Oct 27 '17 15:10 micheleissa

As a developer, it would be preferable to more correctly support parameter names; nameof(o) is not going to be semantically useful in the exception that's thrown.

Reference: https://msdn.microsoft.com/en-us/library/k8a0dfcy(v=vs.110).aspx

Consider something like this, or overloads that would support the same..

T NotNull<T>(T item, string parameterName, string message = null)  { .... }

So that the calling convention would be more akin to

public SomeConstructor(object arg) 
{
    _fieldName = ValidationHelperClassName.NotNull(arg, nameof(arg));
    ....

kcamp avatar Oct 27 '17 15:10 kcamp

@micheleissa This is still assigned to @bookman220. @bookman220 Are you still working on this?

patriksvensson avatar Oct 27 '17 15:10 patriksvensson

@kcamp What would you use parameterName for ? in the case someone passes 'bogus' as the parameterName and it is used in the exception message the result will not be accurate in my opinion.

micheleissa avatar Oct 27 '17 15:10 micheleissa

@micheleissa What @kcamp means is that nameof(o) will always print o. It will never used the original parameter name.

patriksvensson avatar Oct 27 '17 15:10 patriksvensson

@patriksvensson gotcha. Silly me!

micheleissa avatar Oct 27 '17 15:10 micheleissa

So are you supposed to wait for a response from 'bookman220'

micheleissa avatar Oct 28 '17 16:10 micheleissa