Skip to content

xikxekxok/withat

Repository files navigation

Withat

The Readme generated by LLM with minimum human editing. Be patient

Overview

A source code generator, which provides a set of powerful extension methods that allow developers to create updated copies of immutable records with minimal boilerplate code.

With Withat, you can:

  • Generate With methods for record properties.
  • Extend immutable collections like ImmutableArray, ImmutableList, and ImmutableDictionary with intuitive update methods.
  • Ignore specific properties from being extended using attributes.

Key Features and Usage

Automatic With Method Generation

The library automatically generates With methods for all properties of your records. These methods allow you to create updated copies of immutable objects in a concise and expressive way.

Example:

// Define your record
[ExtendedWith]
public record Person(string Name, int Age);

// Create and update instances
var person = new Person("Alice", 30);
var updatedPerson = person.WithName("Bob"); // Creates a new Person with Name = "Bob"
var updatedPerson2 = person.WithAge(x=>x*2); // Creates a new Person with Age = 60

Async Support

All With methods have asynchronous variants, making it easy to integrate with async workflows.

Example:

var updatedPerson = await person.WithAge(Task.FromResult(31));

var updatedPerson2 = await person.WithAge(async x => x + 5);

var updatedPerson3 = await person
    .WithAge(async x => x + 5)
    .WithName("John Doe");

Customizable Property Handling

You can ignore specific properties from being extended by applying the [ExtendedWithIgnore] attribute.

Example:

[ExtendedWith]
public record Person
{
    public required string Name {get; init;}
    [ExtendedWithIgnore]
    public required int Age {get; init;} // This property will not have a `With` method
);

Immutable Collection Extensions

Withat extends immutable collections such as ImmutableArray, ImmutableList, and ImmutableDictionary with methods like With, WithFirst, WithFirstOrDefault, WithAll, and more. These methods make it easy to update elements within immutable collections.

Examples:

  1. ImmutableArray
var array = ImmutableArray.Create(1, 2, 3);

// Update element at index 1
var updatedArray = array.With(index: 1, valueFunc: x => x * 2); // [1, 4, 3]

// Update first element greater than 1
var updatedArray2 = array.WithFirst(x => x > 1, x => x * 2); // [1, 4, 3]

// Update all elements greater than 1
var updatedArray3 = array.WithAll(x => x > 1, x => x * 2); // [1, 4, 6]
  1. ImmutableList
var list = ImmutableList.Create(1, 2, 3);

// Update element at index 1
var updatedList = list.With(index: 1, valueFunc: x => x * 2); // [1, 4, 3]

// Update first element greater than 1
var updatedList2 = list.WithFirstOrDefault(x => x > 1, x => x * 2); // [1, 4, 3]

// Update all elements greater than 1
var updatedList3 = list.WithAll(x => x > 1, x => x * 2); // [1, 4, 6]
  1. ImmutableDictionary
var dictionary = ImmutableDictionary<string, int>.Empty.Add("A", 1).Add("B", 2);

// Update value for key "A"
var updatedDictionary = dictionary.With("A", x => x + 10); // {"A": 11, "B": 2}

// Update value for key "B" asynchronously
var updatedDictionaryAsync = await dictionary.With("B", async x => x + await Task.FromResult(20)); // {"A": 1, "B": 22}

Contributing

We welcome contributions from the community! Here's how you can help:

  1. Report Bugs: If you encounter any issues, please open an issue on GitHub.
  2. Suggest Features: Have an idea for improvement? Let us know by creating a feature request.
  3. Submit Pull Requests: Fork the repository, make your changes, and submit a pull request.

Development Guidelines

  • Follow the existing coding style.
  • Write unit tests for new features.
  • Ensure all tests pass before submitting a PR.

License

This project is licensed under the MIT License.


Contact

For questions or feedback, feel free to reach out:


Thank you for using Withat! We hope it makes your development experience smoother and more enjoyable. 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors