Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/vishnu5104/magic-query-builder

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/vishnu5104/magic-query-builder

  • v0.0.0-20230203163453-c7fadc92bb9d
  • Source
  • Go
  • Socket score

Version published
Created
Source

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.

FAQs

Package last updated on 03 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc