searcher

package module
v0.0.0-...-1d808ec Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2020 License: MIT Imports: 2 Imported by: 0

README

简体中文

Searcher

Searcher is a go package providing simple field access from json document. The interface of this module will be as simple and easy to use as possible

Getting start

Installing

$ go get github.com/markity/searcher

Usage

Given json data here

var jsonString = `
{
  "name":"Markity",
  "age":"16",
  "friends":[
    {
      "name":"Jack",
      "age":17
    },
    {
      "name":"Mary",
      "age":18,
      "email":"3402002560@qq.com"
    }
  ],
  "details":{
    "interests":["golang","python"]
  }
}
`

New a searcher with json data

import "github.com/markity/searcher"

// The error is not nil when the json data is invalid
s, err := searcher.New([]byte(jsonString))
if err != nil {
	fmt.Printf("invalid json data: %v", err)
	return
}

Get a value

// Get a field. The args' type must be int or string
result := s.Query("friends", 0, "name")

// You can check if the filed exists
if !result.Exists() {
    fmt.Println("the result does not exist")
    return
}

// You can check the type of the field. The types contain TypeNumber, TypeBool, TypeString, TypeArray, TypeObject and TypeNull
if result.Type != searcher.TypeString {
    fmt.Println("wrong type of the field")
    return
}

// You can get the value of the field by using GetXXX function(GetInt64, GetUint64, GetFloat64, GetBool, GetString, GetObject, GetArray, GetValue)
// Before getting a value, it's necessary to judge it's type(using wrong "Get function" would cause the program panic)
// For emample, if the result.Type is TypeNumber, you can use GetInt64, GetUint64 and GetFloat64
fmt.Printf("My first friend's name is %s\n", result.GetString())

Change logs

Version: v0.1.1
Data: 2020/2/1
Content:
  Fixed the bug of array length judgment
  More testing
Version: v0.1
Data: 2020/2/1
Content:
  Perfect module function

Documentation

Index

Constants

View Source
const (
	TypeNumber resultType
	TypeBool
	TypeString
	TypeArray
	TypeObject
	TypeNull
)
View Source
const VERSION = "0.1.1"

Variables

This section is empty.

Functions

This section is empty.

Types

type Result

type Result struct {
	Type resultType
	// contains filtered or unexported fields
}

func (*Result) Exists

func (r *Result) Exists() bool

func (*Result) GetArray

func (r *Result) GetArray() []interface{}

func (*Result) GetBool

func (r *Result) GetBool() bool

func (*Result) GetFloat64

func (r *Result) GetFloat64() float64

func (*Result) GetInt64

func (r *Result) GetInt64() int64

func (*Result) GetObject

func (r *Result) GetObject() map[string]interface{}

func (*Result) GetString

func (r *Result) GetString() string

func (*Result) GetUint64

func (r *Result) GetUint64() uint64

func (*Result) GetValue

func (r *Result) GetValue() interface{}

type Searcher

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

func New

func New(data []byte) (*Searcher, error)

New a json searcher. Return error when the json data is invalid

func (*Searcher) Query

func (s *Searcher) Query(args ...interface{}) *Result

Query specific json field. Args' type must be int or string(if not, the function will panic)

Jump to

Keyboard shortcuts

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