Giter VIP home page Giter VIP logo

Comments (8)

itchyny avatar itchyny commented on August 26, 2024 1

You can use gojq.WithFunction to add a custom internal function. You can invoke shell command if you like to within your function implementation.

from gojq.

wader avatar wader commented on August 26, 2024 1

@noperator here is an example of a function that runs a shell command #101 (comment) but as usual with shell and exec, be careful

from gojq.

benatkin avatar benatkin commented on August 26, 2024

Thanks. Also appreciate you closed it, I would rather a repo I use have few open issues than a bunch of out of scope feature requests. I'll blog if I do anything interesting with my newfound knowledge of WithFunction!

from gojq.

noperator avatar noperator commented on August 26, 2024

Hey @benatkin, did you end up using WithFunction to filter values through a shell command? Would love an example if you have one.

from gojq.

noperator avatar noperator commented on August 26, 2024

That's perfect, thanks @wader. @lgfausak suggested in that same issue that one could:

compile in a new module which I have access to as a function from jq

If I understand that comment correctly, then it seems like I could:

  1. Use gojq to compile a module providing a custom function (e.g., cmd())
  2. Write that module to disk at ~/cmd.jq
  3. Import that module into jq to invoke the custom function, like jq -L ~/cmd.jq '"hello" | cmd(["md5"])'

I'm going to play around with that to test my understanding of this whole process. If you happen to see any obvious gaps in that approach, please let me know.

from gojq.

wader avatar wader commented on August 26, 2024

@noperator Your welcome. There is no support for loading "native" modules as such at runtime, instead you can for example build your own version of the gojq cli tool with that function added.

from gojq.

webwurst avatar webwurst commented on August 26, 2024

Loading extensions at runtime is general not easy to achieve with golang? I think if possible it would be very nice to implement new or special features to gojq as an extension in a separate file. To provide a proof-of-concept or try out something and gather feedback. gojq could stay close to upstream. Maybe extensions could be namespaced by default, so no collisions with future jq changes possible.

from gojq.

wader avatar wader commented on August 26, 2024

That is my understanding also but i have no experience doing it. Read a bit about the plugin package out of curiosity and it seems like it requires the plugin to be compiled with exact same golang version and GOPATH etc, not very convenient. Other option i guess would be wasm somehow? but in either case gojq package would require to have some kind of stable plugin API i guess and at what level? WithFunction/WithIterator or lower level?

Maybe if you want to experiment you could import gojq and build some bridge to WithFunction/WithIterator somehow?

Depending on what performance your looking for an alternative could be to mimic something like fastcgi? let an external process get calls as serialized JSON and respond with JSON? using the code i linked to above you can prototype such things like this:

def _process_rpc($cmd): tojson | cmd($cmd) | fromjson;
def json_http($method; $url):
    _process_rpc(["curl", "-X", $method, "-H", "Content-Type: application/json", "-d", "@-", "-o", "-", $url]);

{bla: 123} | json_http("GET"; "https://httpbin.org/anything")
{
    "args": {},
    "data": "{\"bla\":123}",
    "files": {},
    "form": {},
    "headers": {
        "Accept": "*/*",
        "Content-Length": "11",
        "Content-Type": "application/json",
        "Host": "httpbin.org",
        "User-Agent": "curl/7.84.0",
        "X-Amzn-Trace-Id": "Root=1-63669f6d-17a435df1c8948ba322b588c"
    },
    "json": {
        "bla": 123
    },
    "method": "GET",
    "origin": "...",
    "url": "https://httpbin.org/anything"
}

from gojq.

Related Issues (20)

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.