Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Either verbosity #50

Open
savpek opened this issue Jan 21, 2018 · 0 comments
Open

Either verbosity #50

savpek opened this issue Jan 21, 2018 · 0 comments

Comments

@savpek
Copy link

savpek commented Jan 21, 2018

I am testing this library with one project, however either type seems insanely verbose. Heres my code

        public Option<IEnumerable<ImageEvent>, AggregateException> UpdateK8sDeployments (string newImageUri)
        {
            var parsedUri = ImageUriParser.ParseUri (newImageUri);

            var result = _shell.Run ("kubectl get deployments --all-namespaces -o json")
                .Match<Option<IEnumerable<ImageRow>, Exception>> (
                    success =>
                    {
                        var asJObject = JObject.Parse (success);

                        return ParseImageRowsFromJsonResponse (asJObject)
                            .Where (image => SelectOnlyMatchingDeployments (image, parsedUri))
                            .Some<IEnumerable<ImageRow>, Exception> ();

                    }, error => Option.None<IEnumerable<ImageRow>, Exception> (error))
                .Match<Option<IEnumerable<ImageEvent>, AggregateException>> (images =>
                    {
                        var setResult = images
                            .Select (image =>
                                RunSetNewImageCommand (parsedUri, image));

                        if (setResult.Exceptions ().Any ())
                            return Option.None<IEnumerable<ImageEvent>, AggregateException> (new AggregateException (setResult.Exceptions ()));

                        return Option.Some<IEnumerable<ImageEvent>, AggregateException> (setResult.Values ());
                    },
                    error => Option.None<IEnumerable<ImageEvent>, AggregateException> (new AggregateException (error)));

            result.Match(
                    some => _logger.LogInformation($"Updated images: {string.Join(",", some.Select(x => x.Image))}"),
                    none => _logger.LogError(none.ToString()));

            return result;
        }

Basically this parses json that kubernetes returns and then runs bunch of other terminal commands based on that information + logging. Usual stuff. Those complex generics however really reduces readability of code so i am asking advice is there other solutions to reduce noise or even refactor this alltogether to get better readibility? Theres some things i dont grasp yet.

There was issue about implicit casting as option which was denied since it creates its own problems. I agree totally with simple Option type, however on either version of it it reduces generic that and that boilerplate greatly. LanguageExt uses that solution with either which is nice, however that library API is kind of mess on other areas.

Another way i figured out could be to use static imports, however that is blocked since generic is given as part of method not type. Eg. Option<T,TE>.Some() vs Option.Some<T,TE>().

Is only thing left to refactor those generic definitions out with helper methods to shorten calls?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant