Git Product home page Git Product logo

jsref's Introduction

go-jsref

Build Status

GoDoc

JSON Reference Implementation for Go

SYNOPSIS

package jsref_test

import (
  "encoding/json"
  "fmt"
  "log"

  jsref "github.com/lestrrat-go/jsref"
  "github.com/lestrrat-go/jsref/provider"
)

func Example() {
  var v interface{}
  src := []byte(`
{
  "foo": ["bar", {"$ref": "#/sub"}, {"$ref": "obj2#/sub"}],
  "sub": "baz"
}`)
  if err := json.Unmarshal(src, &v); err != nil {
    log.Printf("%s", err)
    return
  }

  // External reference
  mp := provider.NewMap()
  mp.Set("obj2", map[string]string{"sub": "quux"})

  res := jsref.New()
  res.AddProvider(mp) // Register the provider

  data := []struct {
    Ptr string
    Options []jsref.Option
  }{
    {
      Ptr: "#/foo/0", // "bar"
    },
    {
      Ptr: "#/foo/1", // "baz"
    },
    {
      Ptr: "#/foo/2", // "quux" (resolves via `mp`)
    },
    {
      Ptr: "#/foo",   // ["bar",{"$ref":"#/sub"},{"$ref":"obj2#/sub"}]
    },
    {
      Ptr: "#/foo",   // ["bar","baz","quux"]
      // experimental option to resolve all resulting values
      Options: []jsref.Option{ jsref.WithRecursiveResolution(true) },
    },
  }
  for _, set := range data {
    result, err := res.Resolve(v, set.Ptr, set.Options...)
    if err != nil { // failed to resolve
      fmt.Printf("err: %s\n", err)
      continue
    }
    b, _ := json.Marshal(result)
    fmt.Printf("%s -> %s\n", set.Ptr, string(b))
  }

  // OUTPUT:
  // #/foo/0 -> "bar"
  // #/foo/1 -> "baz"
  // #/foo/2 -> "quux"
  // #/foo -> ["bar",{"$ref":"#/sub"},{"$ref":"obj2#/sub"}]
  // #/foo -> ["bar","baz","quux"]
}

Providers

The Resolver object by default does not know how to resolve any reference: You must provide it one or more Providers to look for and resolve external references.

Currently available Providers are:

Name Description
provider.FS Resolve from local file system. References must start with a file:/// prefix
provider.Map Resolve from in memory map.
provider.HTTP Resolve by making HTTP requests. References must start with a http(s?):// prefix

References

Name Notes
go-jsval Validator generator
go-jshschema JSON Hyper Schema implementation
go-jsschema JSON Schema implementation
go-jspointer JSON Pointer implementations

Acknowledgements

  • Boris Burtin

jsref's People

Contributors

koron avatar lestrrat avatar mad-p avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jsref's Issues

http provider doesn't cache results

Hi.
First of all, thanks for a great project!

I've noticed a potential issue:
Here it says Note that once a document is read, it WILL be cached for the duration of this object, unless you call Reset but it doesn't actually call hp.mp.Set() similar to how fs provider does.

I didn't test whether it actually makes requests on 2+ calls but the code looks like it will.

go-jsref.Resolver.Resolve() panics when enum has null-value

My schema has such field

"status": {
                                "type": ["string", "null"],
                                "enum": [
                                    "sent",
                                    "duplicate",
                                    "error",
                                    "invalid",
                                    "rejected",
                                    "unqueued",
                                    "unsubscribed",
                                    null
                                ]
                            }

and go-jsref.Resolver.Resolve() panics

panic: reflect: call of reflect.Value.Interface on zero Value

goroutine 1 [running]:
panic(0x61c800, 0xc4201333a0)
        /usr/local/go/src/runtime/panic.go:500 +0x1a1
reflect.valueInterface(0x0, 0x0, 0x0, 0x1, 0x0, 0x0)
        /usr/local/go/src/reflect/value.go:912 +0x204
reflect.Value.Interface(0x0, 0x0, 0x0, 0x0, 0x0)
        /usr/local/go/src/reflect/value.go:907 +0x44
gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref.traverseExpandRefRecursive(0xc420127cb0, 0xc420127f08, 0x619640, 0xc420138f80, 0x94, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/d.telyukh/go/src/gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref/jsref.go:163 +0x205
gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref.traverseExpandRefRecursive(0xc420127cb0, 0xc420127f08, 0x619640, 0xc420138f20, 0x94, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/d.telyukh/go/src/gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref/jsref.go:183 +0x606
gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref.traverseExpandRefRecursive(0xc420127cb0, 0xc420127f08, 0x619640, 0xc420138830, 0x94, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/d.telyukh/go/src/gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref/jsref.go:183 +0x606
gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref.traverseExpandRefRecursive(0xc420127cb0, 0xc420127f08, 0x619640, 0xc4201387d0, 0x94, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/d.telyukh/go/src/gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref/jsref.go:183 +0x606
gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref.traverseExpandRefRecursive(0xc420127cb0, 0xc420127f08, 0x619640, 0xc420138780, 0x94, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/d.telyukh/go/src/gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref/jsref.go:183 +0x606
gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref.traverseExpandRefRecursive(0xc420127cb0, 0xc420127f08, 0x619640, 0xc420138700, 0x94, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/d.telyukh/go/src/gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref/jsref.go:183 +0x606
gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref.traverseExpandRefRecursive(0xc420127cb0, 0xc420127f08, 0x619640, 0xc4201386a0, 0x94, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/d.telyukh/go/src/gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref/jsref.go:183 +0x606
gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref.traverseExpandRefRecursive(0xc420127cb0, 0xc420127f08, 0x619640, 0xc420138510, 0x94, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/d.telyukh/go/src/gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref/jsref.go:183 +0x606
gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref.traverseExpandRefRecursive(0xc420127cb0, 0xc420127f08, 0x61e960, 0xc4201111a0, 0x15, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/d.telyukh/go/src/gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref/jsref.go:183 +0x606
gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref.(*Resolver).Resolve(0xc420127f08, 0x61e960, 0xc4201111a0, 0x0, 0x0, 0xc420127e08, 0x1, 0x1, 0x0, 0x0, ...)
        /home/d.telyukh/go/src/gitlab.2gis.ru/mailer/mailer-docs/go-jsschema-builder/vendor/github.com/lestrrat/go-jsref/jsref.go:111 +0x37f
main.processFiles(0x662e54, 0xa, 0xc420127f08)

concat multiple references

Hello,

is possible to define a concatenation of references?

something like

{
"foo": [ "bar", { "$ref": "#/sub" }{ "$ref": "#/sub" } ],
"sub": "baz"
}

or

{
"foo": [ "bar", $.concat( { "$ref": "#/sub" }, { "$ref": "#/sub" } ) ],
"sub": "baz"
}

output for "#/foo" should be
[ "bar", "bazbaz" ]

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.