Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
module github.com/blues/jsonata-go

go 1.16
go 1.23.0

toolchain go1.24.2

require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
golang.org/x/crypto v0.38.0 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/net v0.40.0 // indirect
golang.org/x/sync v0.14.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457 // indirect
golang.org/x/term v0.32.0 // indirect
golang.org/x/text v0.25.0 // indirect
golang.org/x/tools v0.33.0 // indirect
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
15 changes: 15 additions & 0 deletions latest/.claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"permissions": {
"allow": [
"Bash(chmod:*)",
"Bash(./run.sh:*)",
"Bash(ls:*)",
"Bash(go build:*)",
"Bash(bash:*)",
"Bash(go get:*)",
"Bash(mkdir:*)",
"Bash(go run:*)"
],
"deny": []
}
}
3 changes: 3 additions & 0 deletions latest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore generated files
/jsonata.go
/latest
218 changes: 218 additions & 0 deletions latest/make-latest/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package main

import (
"fmt"
"go/ast"
"go/types"
"os"
"path/filepath"
"sort"
"strings"
"text/template"

"golang.org/x/tools/go/packages"
)

type Symbol struct {
Name string
Type string
Doc string
}

type ShimData struct {
ImportPath string
Version string
Types []Symbol
Vars []Symbol
Consts []Symbol
Funcs []Symbol
}

func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: make-latest ../../v1.2.3")
fmt.Println("(creates ../jsonata.go with wrappers from the package in the v1.2.3 directory)")
os.Exit(1)
}

sourcePath := os.Args[1]

// Extract version from path or use the final directory name
version := extractVersionFromPath(sourcePath)

// If version not found in path, use the final directory name
if version == "" {
// Get the last part of the path
version = filepath.Base(sourcePath)
}

fmt.Printf("Inspecting jsonata package at: %s (version %s)\n", sourcePath, version)

// Construct import path for the package
importPath := fmt.Sprintf("github.com/blues/jsonata-go/%s", version)
fmt.Printf("Using import path: %s\n", importPath)

// Get exported symbols
data, err := getExportedSymbols(importPath, version)
if err != nil {
fmt.Printf("Error inspecting package: %v\n", err)
os.Exit(1)
}

fmt.Printf("Found %d exported symbols\n",
len(data.Types)+len(data.Vars)+len(data.Consts)+len(data.Funcs))

err = generateWrapper(data)
if err != nil {
fmt.Printf("Error generating wrapper: %v\n", err)
os.Exit(1)
}

fmt.Println("Successfully generated jsonata.go wrapper")
}

// extractVersionFromPath tries to extract the version from the path
func extractVersionFromPath(path string) string {
// Try to find a version pattern like v1.2.3 in the path
parts := strings.Split(path, "/")
for _, part := range parts {
if strings.HasPrefix(part, "v") && len(part) > 1 {
// Check if the rest of the string could be a version number
if _, err := fmt.Sscanf(part[1:], "%f", &struct{ f float64 }{}); err == nil {
return part
}
}
}
return ""
}

func getExportedSymbols(importPath string, version string) (*ShimData, error) {
cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedTypesInfo | packages.NeedImports | packages.NeedName,
Env: os.Environ(),
Dir: ".", // start from current module
}

pkgs, err := packages.Load(cfg, importPath)
if err != nil {
return nil, fmt.Errorf("error loading package: %v", err)
}

if packages.PrintErrors(pkgs) > 0 || len(pkgs) == 0 {
return nil, fmt.Errorf("no package found for %s", importPath)
}

pkg := pkgs[0].Types

data := &ShimData{
ImportPath: importPath,
Version: version,
}

scope := pkg.Scope()
for _, name := range scope.Names() {
// Check if the name starts with an uppercase letter (exported)
if !ast.IsExported(name) {
continue
}

obj := scope.Lookup(name)
symbol := Symbol{Name: name}

switch obj.(type) {
case *types.TypeName:
symbol.Type = "Type"
data.Types = append(data.Types, symbol)
case *types.Var:
symbol.Type = "Var"
data.Vars = append(data.Vars, symbol)
case *types.Const:
symbol.Type = "Const"
data.Consts = append(data.Consts, symbol)
case *types.Func:
symbol.Type = "Func"
data.Funcs = append(data.Funcs, symbol)
}
}

// Sort each category by name
sort.Slice(data.Types, func(i, j int) bool {
return data.Types[i].Name < data.Types[j].Name
})
sort.Slice(data.Consts, func(i, j int) bool {
return data.Consts[i].Name < data.Consts[j].Name
})
sort.Slice(data.Vars, func(i, j int) bool {
return data.Vars[i].Name < data.Vars[j].Name
})
sort.Slice(data.Funcs, func(i, j int) bool {
return data.Funcs[i].Name < data.Funcs[j].Name
})

return data, nil
}

func generateWrapper(data *ShimData) error {
// Create template functions
funcMap := template.FuncMap{
"join": func(strs []string, sep string) string {
return strings.Join(strs, sep)
},
}

// Create and parse template
t := template.New("wrapper").Funcs(funcMap)

const templateText = `// Copyright Blues Inc. All rights reserved.

// Package jsonata is a query and transformation language for JSON.
// This is a wrapper package that provides the same interface as the v{{.Version}} package.
// Generated automatically.
package jsonata

import (
latest "{{.ImportPath}}"
)

// Types
{{range .Types}}// {{.Name}} is a type from the latest JSONata package
type {{.Name}} = latest.{{.Name}}
{{end}}

// Constants
{{range .Consts}}// {{.Name}} is a constant from the latest JSONata package
const {{.Name}} = latest.{{.Name}}
{{end}}

// Variables
{{range .Vars}}// {{.Name}} is a variable from the latest JSONata package
var {{.Name}} = latest.{{.Name}}
{{end}}

// Functions
{{range .Funcs}}// {{.Name}} is a function from the latest JSONata package
var {{.Name}} = latest.{{.Name}}
{{end}}
`

t, err := t.Parse(templateText)
if err != nil {
return fmt.Errorf("error parsing template: %v", err)
}

// Create output file
outputPath := filepath.Join("..", "jsonata.go")
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("error creating output file %s: %v", outputPath, err)
}
defer file.Close()

// Execute template
err = t.Execute(file, data)
if err != nil {
return fmt.Errorf("error executing template: %v", err)
}

return nil
}
Binary file added latest/make-latest/make-latest
Binary file not shown.
10 changes: 10 additions & 0 deletions v1.5.4/.claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"permissions": {
"allow": [
"Bash(ls)",
"Bash(go:*)",
"Bash(grep:*)"
],
"deny": []
}
}
Loading
Loading