Giter VIP home page Giter VIP logo

magic-query-builder's Introduction

Magic-Query-Builder

This is a meta-language query builder implemented in Go. The meta-language consists of string-format text where . (dot) is the separator between keys (or different levels in a JSON) and [] is a special keyword that indicates the word that immediately precedes is a list.

Applied implementation perspective

This is a Go program that constructs a JSON query from a list of filters and saves the result to a file named "generated_query.json".

The program starts with importing several packages, including "fmt", "os", "strings", and "encoding/json".

A "Query" type is defined as a map with string keys and interface{} values, which can hold any Go data type.

The main function of the program is "MagicBuildQuery". It takes a slice of strings as input, each string being a filter in the format of "key=value". The function constructs a Query type from these filters and returns it.

The function works by first creating an empty Query map, and adding a key "query" to it. Then, for each filter, the function splits the string by "=", trims the whitespaces of the two parts, and splits the key by ".". The value part is then assigned to the corresponding key in the map, using the keys as the path to the value.

If any key contains "[]", it means the value is an array, and the function adds a new key "match" to the current map, with its value being another map, which then has a key "eq" with the value of the filter.

Finally, the constructed Query is converted to a JSON string using the "json.MarshalIndent" function and saved to a file using the "os.Create" function.

In the "main" function, a slice of filters is defined, and the "MagicBuildQuery" is called with it as the input. The returned Query is then converted to a JSON string and saved to a file.

If any error occurs during the process, the program will print an error message and return. If everything goes well, the program will print a message indicating success.

Issue with the Output

As i'm noob to this go lang so in this approch there is a problem in the function MagicBuildQuery() seems to be working as intended, as it splits the input string by "." and treats each segment as a key in a nested map. When it encounters a segment that ends with "[]", it treats the key as an array, which will contain maps that are matched against the conditions defined by the key-value pairs.

In the input "interests.[].sport.name": "football", the keys are split into the following segments: ["interests", "", "sport", "name"]. The second segment, "", is not a valid key. So in the output file it also generate "" as key.

magic-query-builder's People

Contributors

vishnu5104 avatar

Watchers

 avatar

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.