subset

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2022 License: MIT Imports: 4 Imported by: 0

README

Query for object subset

Documents can become quite large after a while. But if only a small part of the document is needed, it creates unnecessary load. This parser can be used to generate a subset by means of a query. This would allow the API to return only the required data. E.g. the roles of a user.

The language

The syntax of this language is simple: path.field_name=subset_field_name. You can concatenate fields to a subset with the separator , e.g. contact.email=email,contact.phone=phone to get the subset like {email: "___", phone: "___"}.

Example

import (
	"github.com/StevenCyb/goapiutils/object/subset"

	"go.mongodb.org/mongo-driver/mongo/options"
)
// ...

  subsetExpression := r.URL.Query().Get("subset")

  // ...

  parser := subset.NewParser(nil)
  resultDataSubset, err := parser.Parse(subsetExpression, resultData)

  // ...
  
  json.NewEncoder(w).Encode(resultDataSubset)

Documentation

Index

Constants

View Source
const (
	SkipType          tokenizer.Type = "SKIP"
	JoinType          tokenizer.Type = ","
	PathSeparatorType tokenizer.Type = "."
	AssignmentType    tokenizer.Type = "ASSIGNMENT"
	FieldNameType     tokenizer.Type = "FIELD_NAME"
)

Types that are used in this parser.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser provides the logic to parse rsql statements.

func NewParser

func NewParser() *Parser

NewParser creates a new parser.

func (*Parser) Parse

func (p *Parser) Parse(query string, fullObject interface{}) (interface{}, error)

Parse a given query.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL