Souvikns / parser-go

AsyncAPI parser for golang
Apache License 2.0
0 stars 0 forks source link

support for resolving json references #3

Open Souvikns opened 6 months ago

Souvikns commented 6 months ago

Some libraries we can checkout

Souvikns commented 6 months ago

https://github.com/lestrrat-go/jsref

Has minimal support for resolving json references, but needs to manually scrape the references from the file.

Example code


package main

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

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

func Example() {
    var v interface {}
    src := []byte(`
    {
        "asyncapi": "3.0.0",
        "channel": {
            "$ref": "#/components/chnl"
        },
        "components": {
            "chnl": {
                "address": "https://github.com"
            }
        }
    }
    `)
    if err :=json.Unmarshal(src, &v); err != nil {
        log.Printf("%s", err)
        return
    }

    mp := provider.NewMap()
    res := jsref.New()
    res.AddProvider(mp)
    // data := []struct {
    //  Ptr string
    //  Options []jsref.Option
    // }{}
    result, err := res.Resolve(v, "#/components/chnl")
    if err != nil {
        log.Printf("%s", err)
    }
    b, _ := json.Marshal(result)
    fmt.Println(string(b))
}

func main(){
    Example()
}