Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Augmenting and reducing data structures

by sciurius (Sexton)
on Apr 23, 2021 at 12:02 UTC ( [id://11131654]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Augmenting and reducing data structures
in thread Augmenting and reducing data structures

My question originates from a large software project that has a complex configuration structure.

Something similar to:

# cfg1 $config = { "common" => { debug => 1, logfile => "/var/log/project.log", path => "/opt/projects/bin", }, "project_1" => { name => "Project 1", tasks => [ { name => "Prepare", program => "p1prep", arguments => [ "--init", '--default' ], logfile => "/var/log/p1prep.log", }, { name => "Work", program => "p1", arguments => [ "--collect" ], logfile => "/var/log/p1.log", env => { DISPLAY => undef, }, }, { name => "Wrapup", program => "signal", arguments => [ "p1", "finished" ], }, ] }, "project_2" => { "and" => [ "so", "on", "..." ], }, };

A particular customer could supply her own config, e.g.

# cfg2 $config = { "common" => { debug => 0, logfile => "/var/log/project.log", path => "/opt/projects/bin", }, "project_1" => { name => "Project 1", tasks => [ { name => "Prepare", program => "p1prep", arguments => [ "--init", '--default' ], logfile => "/var/log/p1prep.log", }, { name => "Work", program => "p1", arguments => [ "--collect", "--brief" ], logfile => "/var/log/p1.log", env => { DISPLAY => undef, }, }, { name => "Wrapup", program => "signal", arguments => [ "p1", "finished" ], }, ] }, "project_2" => { "and" => [ "so", "on", "..." ], }, };

But instead of supplying a full config, it suffices to supply a smaller set of modifications:

# cfgaug $config = { "common" => { debug => 0 }, "project_1" => { tasks => [ { name => "Prepare" }, { name => "Work", arguments => [ "--collect", "--brief" ], }, { name => "Wrapup" }, ] }, };

Applying #cfgaug to #cfg1 will result in #cfg2.

Since several of these augmentations are possible, the need arises to produce a single 'delta' that augments the base configuration #cfg1 to the actual state.

Replies are listed 'Best First'.
Re^4: Augmenting and reducing data structures
by LanX (Saint) on Apr 23, 2021 at 16:17 UTC
    1. it seems you confused cfgaug with cfgdelta following your own terminology in the OP
    2. you've added more complex and fuzzy requirements by altering arrays:

      [ "--collect" ] becomes [ "--collect", "--brief" ],

    3. you haven't told us at all how to treat arrays ...

      e.g. do they represent sets were the order doesn't matter?

    You've been provided with two solutions, Hippo mentioned modules I've sketched an algorithm.

    I think it's time for you to wet your feet and come up with an SSCCE plus test suite for this special kind of data.

    Unfortunately we are not telepathic, we can't help you with fuzzy requirements of an alien cfg format.

    I strongly recommend you implementing the recursive walker (many examples in the archives) which dives into your data and handles scalars, arrays and hashes by type.

    Then you play around and handle special edge cases AND semantics, like one array is supposed to be a set, the other one is ordered an so on ...

    That way you'll also get at least a validator for your "user supplied" cfg-format.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      There are a couple of additional constraints that make the tast easier:

      • The corresponding data types of the old and new structures match;
      • There are no undefined values involved.

      Nevertheless further research has show that there are a lot of edge cases that make the task complicated.

      I'm investigating the Struct::Diff approach, as well as several other suggestions from this thread. Thanks a lot for the input!

        I hope you understood my point that writing a validator as a first step would not only constitute the foundation but more than half the work needed to any possible diff-solution.

        Anything else is doomed to fail.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131654]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-26 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found