Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ogulcanturan/ogu.extensions.saferesult

Safely creates collections from separated strings
https://github.com/ogulcanturan/ogu.extensions.saferesult

Last synced: 12 days ago
JSON representation

Safely creates collections from separated strings

Awesome Lists containing this project

README

        

# Header Ogu.Extensions.SafeResult

[![.NET](https://github.com/ogulcanturan/Ogu.Extensions.SafeResult/actions/workflows/dotnet.yml/badge.svg?branch=master)](https://github.com/ogulcanturan/Ogu.Extensions.SafeResult/actions/workflows/dotnet.yml)
[![NuGet](https://img.shields.io/nuget/v/Ogu.Extensions.SafeResult.svg?color=1ecf18)](https://nuget.org/packages/Ogu.Extensions.SafeResult)
[![Nuget](https://img.shields.io/nuget/dt/Ogu.Extensions.SafeResult.svg?logo=nuget)](https://nuget.org/packages/Ogu.Extensions.SafeResult)

## Introduction

Ogu.Extensions.SafeResult is a library that safely creates collections from separated strings.

## Features

- **Safe Parsing:** Safely parse separated strings into collections without encountering exceptions.
- **Customization:** Specify whether parsing should stop on the first failure or continue parsing subsequent elements.
- **Supports:** List, HashSet, and Dictionary (extensions: ToSafeList, ToSafeHashSet, ToSafeDictionary)

## Installation

You can install the library via NuGet Package Manager:

```bash
dotnet add package Ogu.Extensions.SafeResult
```

## Usage

**HashSet:**
```csharp
public class Request
{
[FromQuery(Name = "ids"), JsonIgnore]
public string Ids { get; set; } = "1,2,3 , 4, a, 6";

[BindNever, JsonIgnore]
public ISafeResult> IdList => _idList ??= SafeResult.HashSet(Ids, stopOnFailure: true);

private ISafeResult> _idList;
}
```

IdList.Result =>

```bash
[1,2,3,4]
```

**EnumHashSet:**
```csharp
public enum Color
{
Red,
Green,
Blue,
}

public class Request
{
[FromQuery(Name = "ids"), JsonIgnore]
public string Ids { get; set; } = "0, a, 16, 1, 2";

[BindNever, JsonIgnore]
public ISafeResult> IdList => _idList ??= SafeResult.EnumHashSet(Ids, stopOnFailure: true);

private ISafeResult> _idList;
}
```

IdList.Result =>

```bash
[Red]
```

**OrderedDictionary:**
```csharp
public class Request
{
[FromQuery(Name = "ids"), JsonIgnore]
public string Ids { get; set; } = "1,2,3 , 4, a, 6";

[BindNever, JsonIgnore]
public ISafeResult> IdList => _idList ??= SafeResult.OrderedDictionary(Ids, stopOnFailure: false);

private ISafeResult> _idList;
}
```

IdList.Result =>

```bash
{
"1":0,
"2":1,
"3":2,
"4":3,
"6":4
}
```

**List:**
```csharp
public class Request
{
[FromQuery(Name = "ids"), JsonIgnore]
public string Ids { get; set; } = "Item1,Item2,Item3,item3,25";

[BindNever, JsonIgnore]
public ISafeResult> IdList => _idList ??= SafeResult.List(Ids, stopOnFailure: false);

private ISafeResult> _idList;
}
```

IdList.Result =>

```bash
[25]
```