Git Product home page Git Product logo

dmerge's Introduction

Dmerge

A mini merge DSL for data overlays.

Dmerge is a lightweight alternative to the NixOS module system to wrangle data. It aims to give you alternative semantics, currently not available in the ecosystem, when you need them.

Semantics

  • Simple semantics: merge :: lhs -> rhs -> final
  • Monotonistic on the Dataspine
  • Expressive Array Merge Decorations
  • Typed-by-Precedent

Simple semantics

# nix repl

> :lf github:divnix/dmerge

> :p merge { foo = "bar"; } { foo = "baz"; }
{ foo = "baz"; }

> :p merge { foo = [1]; } { foo = append [2]; }
{ foo = [ 1 2 ]; }

> :p merge { foo = [1]; } { foo = prepend [2]; }
{ foo = [ 2 1 ]; }

# update [idx] updates idx on lhs
> :p merge { foo = [1]; } { foo = update [0] [2]; }
{ foo = [ 2 ]; }

# recurses the arrays and attribute sets
> :p merge { foo = [{egg = {color = "yellow";};}]; } { foo = update [0] [{egg = {color = "green";};}]; }
{ foo = [ { egg = { color = "green"; }; } ]; }

# supports associative array merges
> :p merge { people = [{name = "bert"; age = 42;}]; } {people = updateOn "name" [{name = "bert"; age = 43;}]; }
{ people = [ { age = 43; name = "bert"; } ]; }

# chaining
> WithMichi = chainable {michelangelo = { age = 548; };}
> WithRemi = chainable {rembrandt = { age = 417; };}
> WithLeo = chainable {davinci = { age = 571; };}
> mkParty = chainMerge
> :p mkParty WithMichi WithRemi WithLeo {me = {age = 35;};}
{ davinci = { age = 571; }; me = { age = 35; }; michelangelo = { age = 548; }; rembrandt = { age = 417; }; }

Monotonistic on the Dataspine

Dmerge never destroys an Attribute Set or an Array. Unlike simple values, they are considered the dataspine and must be protected.

If you have a use case for destroying an Attribute Set or Array or plainly overriding an Array, consider better factorizing your left hand side, instead. It's a symptom of poor factorization.

Expressive Array Merge Decorations

In the above examples prooving its simplicity, you already got to know the full instruction set of Dmerge to specify the array merge strategy on the right hand side: append, prepend, update & updateOn.

If you don't control the right hand side, but know it's structure, you can decorate it:

merge lhs (decorate rhs { people = updateOn "name";})

Typed by Precedent

You can't modify the type of a left hand side node or leave in the right hand side.

Tests

Check out the tests for more examples.

Implementor's Note

This repo has no flake.lock. Chose the dependencies you see fit; assert compatibilty — chances are high, you'll be fine, though.

Example Use:

# flake.nix
{
  inputs.nixpkgs.url = "github:nixos/nixpkgs";
  inputs.dmerge = {
    url = "github:divnix/dmerge";
    inputs.nixpkgs.follows = "nixpkgs";
    inputs.namaka.follows = ""; # testing only dependency
  };

  /* ... */
}

Acknowledgements

dmerge's People

Contributors

blaggacao avatar dantefromhell avatar figsoda avatar nrdxp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

ilkecan

dmerge's Issues

Inconsistent behavior on new keys in RHS

Defining new keys works as expected if you don't use merge functions in the RHS:

merge {} {
  new = [];
  nested.new = [];
}
{
  "nested": {
    "new": []
  },
  "new": []
}

But if you use a merge function it fails:

merge {} {
  new = append [];
}
error: evaluation aborted with the following error message: '
       a fresh right-hand-side cannot be an array merge function
       at 'new':
         - rhs: lambda @ …

Ok, understandable. Unexpectedly it works if the new key is nested, although the result is the append lambda itself. This is probably not what the user wants:

merge {} {
  nested.new = append [];
}
{ nested = { new = <LAMBDA>; }; }

This seem inconsistent. Defining a new key with a merge function in the RHS should either always fail with a helpful message or always succeed.

I tend to prefer the latter because that would be consistent with the behavior we observe if no merge functions are used in the RHS (see first snippet).

The UX benefit would be that it always just works™.

One drawback could be that you can make less guarantees about the outcome from looking at the LHS. However, we can already not make any guarantees about which new keys will exist in the result since that depends on whether the new keys in the RHS use merge functions or not, and whether they are nested or not, which the LHS does not know. But the benefits of the data spine would still hold if data-merge accepted both scenarios in the RHS.

The real life problem is that you may not always know which keys exist in the LHS if you get it from elsewhere, like when writing a library. Currently it is necessary to pre-create the new keys in the LHS first to allow merging in possibly new keys with merge functions from the RHS (runnable example):

{
  inputs.data-merge.url = "github:divnix/data-merge";

  outputs = { nixpkgs, data-merge, ... }: {
    result = with data-merge; let
      lhs = {}; # we may not know this value
      rhs = { new = append []; };
      # defeats the purpose of data-merge
      prepopulatedLhs = nixpkgs.lib.recursiveUpdate lhs {
        new = lhs.new or [];
      };
    in merge prepopulatedLhs rhs; # ok
    # in merge lhs rhs; # fails
  };
}

List updating is broken on latest commit

nix-repl> dm = inputs.data-merge 

nix-repl> dm.merge { a = [ 6 ]; } { a = dm.update [ 0 ] [ 7 ]; }
{ a = [ ... ]; }

nix-repl> (dm.merge { a = [ 6 ]; } { a = dm.update [ 0 ] [ 7 ]; }).a
[ { ... } ]

The first item in that list should be 7.

cc @blaggacao this is resulting in syntax errors in nomad jobs built in cardano-world.

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.