Files
upstream-ory-keto/oryx/jsonx/embed.go
upstream-mirror 9456a4e922
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:08:34 +05:00

114 lines
2.3 KiB
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package jsonx
import (
"encoding/base64"
"encoding/json"
"net/url"
"slices"
"strconv"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"github.com/ory/x/osx"
)
type options struct {
ignoreKeys []string
onlySchemes []string
}
type OptionsModifier func(*options)
func newOptions(o []OptionsModifier) *options {
opt := &options{}
for _, f := range o {
f(opt)
}
return opt
}
func WithIgnoreKeys(keys ...string) OptionsModifier {
return func(o *options) {
o.ignoreKeys = keys
}
}
func WithOnlySchemes(scheme ...string) OptionsModifier {
return func(o *options) {
o.onlySchemes = scheme
}
}
func EmbedSources(in json.RawMessage, opts ...OptionsModifier) (out json.RawMessage, err error) {
out = make([]byte, len(in))
copy(out, in)
if err := embed(gjson.ParseBytes(in), nil, &out, newOptions(opts)); err != nil {
return nil, err
}
return out, nil
}
func embed(parsed gjson.Result, parents []string, result *json.RawMessage, o *options) (err error) {
if parsed.IsObject() {
parsed.ForEach(func(k, v gjson.Result) bool {
err = embed(v, append(parents, strings.ReplaceAll(k.String(), ".", "\\.")), result, o)
return err == nil
})
if err != nil {
return err
}
} else if parsed.IsArray() {
for kk, vv := range parsed.Array() {
if err = embed(vv, append(parents, strconv.Itoa(kk)), result, o); err != nil {
return err
}
}
} else if parsed.Type != gjson.String {
return nil
}
if len(parents) > 0 && slices.Contains(o.ignoreKeys, parents[len(parents)-1]) {
return nil
}
loc, err := url.ParseRequestURI(parsed.String())
if err != nil {
// Not a URL, return
return nil
}
if len(o.onlySchemes) == 0 {
if loc.Scheme != "file" && loc.Scheme != "http" && loc.Scheme != "https" && loc.Scheme != "base64" {
// Not a known pattern, ignore
return nil
}
} else if !slices.Contains(o.onlySchemes, loc.Scheme) {
// Not a known pattern, ignore
return nil
}
contents, err := osx.ReadFileFromAllSources(loc.String())
if err != nil {
return err
}
encoded := base64.StdEncoding.EncodeToString(contents)
key := strings.Join(parents, ".")
if key == "" {
key = "@"
}
interim, err := sjson.SetBytes(*result, key, "base64://"+encoded)
if err != nil {
return err
}
*result = interim
return
}