jsonld_helper

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: MIT Imports: 6 Imported by: 1

README

cloudmate logo

JSON-LD Helper

by Cloudmate


Golang

Go Test

Why use this library?

When receive JSON-LD,

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "name": "juunini",
  "type": "Person",
  "id": "juunini"
}

is equals

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "as:name": "juunini",
  "type": "Person",
  "@id": "juunini"
}

and it also equals

[
  {
    "https://www.w3.org/ns/activitystreams#name": [
      {
        "@value": "juunini"
      }
    ],
    "@id": "juunini",
    "https://www.w3.org/ns/activitystreams#type": [
      {
        "@value": "Person"
      }
    ]
  }
]

when receive any shape of JSON-LD, we can parse and use same interface.

Installation

go get github.com/cloudmatelabs/go-jsonld-helper

Usage

import (
  jsonld_helper "github.com/cloudmatelabs/go-jsonld-helper"
)

var doc = map[string]any{
	"@context": []any{
		"https://www.w3.org/ns/activitystreams",
		map[string]any{
			"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
		},
	},
	"@id":     "https://mastodon.social/users/juunini",
	"as:type": "Person",
	"url":     "https://mastodon.social/@juunini",
	"as:image": map[string]any{
		"@type":        "Image",
		"as:mediaType": "image/png",
		"url":          "https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png",
	},
	"manuallyApprovesFollowers": "true",
}

jsonld, err := jsonld_helper.ParseJsonLD(doc, nil)
if err != nil {
  // handle error
}

imageType := jsonld.ReadKey("image").ReadKey("mediaType").StringOrElse("")
// image/png
imageURL := jsonld.ReadKey("image").ReadKey("url").StringOrThrow()
// https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png
id := jsonld.ReadKey("id").Get()
id := jsonld.ReadKey("@id").Get()
// https://mastodon.social/users/juunini
messageType := jsonld.ReadKey("type").Get()
messageType := jsonld.ReadKey("@type").Get()
// Person
manuallyApprovesFollowers := jsonld.ReadKey("manuallyApprovesFollowers").BooleanOrElse(false)
// true

Methods and Properties

import "github.com/piprate/json-gold/ld"

jsonld_helper.ParseJsonLD (doc any, options *ld.JsonLdOptions) (JsonLDReader, error)

.Value() any
.Length() int

.ReadKey(string) JsonLDReader
.ReadIndex(int) JsonLDReader

.Get() any
.GetOrElse(any) any
.GetOrThrow(error) (any, error)

.StringOrElse(string) string
.StringOrThrow(error) (string, error)

.BoolOrElse(bool) bool
.BoolOrThrow(error) (bool, error)

.IntOrElse(int) int
.IntOrThrow(error) (int, error)

.FloatOrElse(float64) float64
.FloatOrThrow(error) (float64, error)

License

MIT

But, this library use json-gold.
json-gold is licensed under the Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JsonLDReader

type JsonLDReader interface {
	Value() any
	Length() int

	ReadKey(string) JsonLDReader
	ReadIndex(int) JsonLDReader

	Get() any
	GetOrElse(any) any
	GetOrThrow(error) (any, error)

	StringOrElse(string) string
	StringOrThrow(error) (string, error)

	BoolOrElse(bool) bool
	BoolOrThrow(error) (bool, error)

	IntOrElse(int) int
	IntOrThrow(error) (int, error)

	FloatOrElse(float64) float64
	FloatOrThrow(error) (float64, error)
}

func ParseJsonLD

func ParseJsonLD(value any, options *ld.JsonLdOptions) (JsonLDReader, error)

type Nothing

type Nothing struct {
	Error error
}

func (Nothing) BoolOrElse

func (n Nothing) BoolOrElse(defaultValue bool) bool

func (Nothing) BoolOrThrow

func (n Nothing) BoolOrThrow(err error) (bool, error)

func (Nothing) FloatOrElse

func (n Nothing) FloatOrElse(defaultValue float64) float64

func (Nothing) FloatOrThrow

func (n Nothing) FloatOrThrow(err error) (float64, error)

func (Nothing) Get

func (n Nothing) Get() any

func (Nothing) GetOrElse

func (n Nothing) GetOrElse(defaultValue any) any

func (Nothing) GetOrThrow

func (n Nothing) GetOrThrow(err error) (any, error)

func (Nothing) IntOrElse

func (n Nothing) IntOrElse(defaultValue int) int

func (Nothing) IntOrThrow

func (n Nothing) IntOrThrow(err error) (int, error)

func (Nothing) Length

func (n Nothing) Length() int

func (Nothing) ReadIndex

func (n Nothing) ReadIndex(int) JsonLDReader

func (Nothing) ReadKey

func (n Nothing) ReadKey(string) JsonLDReader

func (Nothing) StringOrElse

func (n Nothing) StringOrElse(defaultValue string) string

func (Nothing) StringOrThrow

func (n Nothing) StringOrThrow(err error) (string, error)

func (Nothing) Value

func (n Nothing) Value() any

type Reader

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

func (Reader) BoolOrElse

func (r Reader) BoolOrElse(defaultValue bool) bool

func (Reader) BoolOrThrow

func (r Reader) BoolOrThrow(err error) (bool, error)

func (Reader) FloatOrElse

func (r Reader) FloatOrElse(defaultValue float64) float64

func (Reader) FloatOrThrow

func (r Reader) FloatOrThrow(err error) (float64, error)

func (Reader) Get

func (r Reader) Get() any

func (Reader) GetOrElse

func (r Reader) GetOrElse(defaultValue any) any

func (Reader) GetOrThrow

func (r Reader) GetOrThrow(err error) (any, error)

func (Reader) IntOrElse

func (r Reader) IntOrElse(defaultValue int) int

func (Reader) IntOrThrow

func (r Reader) IntOrThrow(err error) (int, error)

func (Reader) Length

func (r Reader) Length() int

func (Reader) ReadIndex

func (r Reader) ReadIndex(index int) JsonLDReader

func (Reader) ReadKey

func (r Reader) ReadKey(key string) JsonLDReader

func (Reader) StringOrElse

func (r Reader) StringOrElse(defaultValue string) string

func (Reader) StringOrThrow

func (r Reader) StringOrThrow(err error) (string, error)

func (Reader) Value

func (r Reader) Value() any

Jump to

Keyboard shortcuts

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