ianka / xjson.tcl

A Tcl library that validates, collects, composes JSON data against supplied schemas and to/from Tcl data.
Other
5 stars 0 forks source link

xjson::rpatch replace and other same actions with glob match #1

Open iicuken opened 1 year ago

iicuken commented 1 year ago

Thank you sir for u interesting library. I found it very useful. My use case: I have hi compound json with very big level nestng. I need to replace some long string value inside this json to another long string value. I am doing xjson::decode, getting long nested dictionary.And then in my vim editor i copy this dictionary to another line, and found in this line by search place where that string to replace is. And then i am making small change in that string, just to create patch like it described in you docks. package require xjson

set old_json $SOME_LONG_DICT set new_json $SOME_LONG_NEW_DICT

set diff [xjson::diff $new_json $old_json]

And then i am getting patch that allow me easily replace string in old big json with 'rpatch replace' without addressing it directly like in php for example. PERFECT!!

But problem is that i can replace only that exactly matched string in patch and i am want a GLOB MATCHED string. So i took source code, file patch.tcl, found rpatch function and replace check in line 223:

if {$patchParam2 ne $decodedJson} to if {![string match $patchParam2 $decodedJson]}

And then i replace target string in patch to glob "*small_string*". And IT WORKED!! It allow me to obtain all my json in database without creating multiple patches. And it PASSES ALL TESTS!!

Mb u should consider feature like this in u excellent library?? Thank you.

ianka commented 1 year ago

I try to understand the type of problem you are solving with this stunt. Maybe there's an even better way to do it?

iicuken commented 1 year ago

In short: there is a lot of huge json оbjects with same structure and different content. I want to replace one long string value deep inside json by another string.

Its very difficult or impossible address this string directly because a lot of json nesting.

So i use perfect mechanism u provide in u library - patch generation.

But for now patch can be applied to exactly matched object or string.
My string value may be a little defferent from one object to another, and they are all have one common word inside them. For example 'Contacts'.

So i want ability to not generate patch for every variant of object or string but use one patch with glob regexps, or regexps itself. Like i did here: xjson::repatch $myxjson { ...... replace {string {mynew_addr_html}} {string {*Contacts*}}} I successfully change 180 blocks of json content on my site with this feature thanks to this great library.

ianka commented 1 year ago

I understood that, yes. I only wonder how to factor out this functionality you added so there's more than this one use case for it.

(Also, as an additional snag, I wonder if I should change the patch format so it encodes to RFC6902 compatible patches.)