Skip to content

Commit

Permalink
feat(files): support javascript for dynamic data
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Dec 12, 2022
1 parent 2fc183e commit f1101f4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
19 changes: 19 additions & 0 deletions examples/simple/data/002-items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// The response of the "data" function is what's put in the database
function data() {
return {
meta: {
createdKey: 'created_at',
updatedKey: 'updated_at'
},
data: [
{
item: 2,
some_date: new Date(),
},
{
item: 2,
some_date: new Date(),
},
]
}
}
9 changes: 8 additions & 1 deletion examples/simple/migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main
import (
"fmt"
"os"
"time"

"github.com/cenkalti/backoff/v4"
"gorm.io/driver/mysql"
Expand All @@ -27,13 +28,19 @@ type User struct {
Password string
}

type Item struct {
gorm.Model
Item int
SomeDate time.Time
}

func execute(dialector gorm.Dialector) error {
db, err := gorm.Open(dialector, &gorm.Config{})
if err != nil {
return err
}

if err := db.AutoMigrate(&User{}); err != nil {
if err := db.AutoMigrate(&User{}, &Item{}); err != nil {
return err
}

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/cenkalti/backoff/v4 v4.2.0
github.com/dop251/goja v0.0.0-20221118162653-d4bf6fde1b86
github.com/gin-contrib/requestid v0.0.6
github.com/gin-gonic/gin v1.8.1
github.com/mrsimonemms/gin-structured-logger v0.1.0
Expand All @@ -19,11 +20,13 @@ require (
)

require (
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,16 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo=
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/dop251/goja v0.0.0-20211022113120-dc8c55024d06/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
github.com/dop251/goja v0.0.0-20221118162653-d4bf6fde1b86 h1:E2wycakfddWJ26v+ZyEY91Lb/HEZyaiZhbMX+KQcdmc=
github.com/dop251/goja v0.0.0-20221118162653-d4bf6fde1b86/go.mod h1:yRkwfj0CBpOGre+TwBsqPV0IH0Pk73e4PXJOeNDboGs=
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
github.com/dop251/goja_nodejs v0.0.0-20211022123610-8dd9abb0616d/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -95,6 +103,8 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
Expand Down
35 changes: 34 additions & 1 deletion pkg/gobblr/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import (
"regexp"
"strings"
"time"

"github.com/dop251/goja"
)

type FileType string

const (
FileTypeJSON FileType = "json"
FileTypeJS FileType = "js"
)

var AllowedFileTypes = map[FileType]struct{}{
FileTypeJSON: {},
FileTypeJS: {},
}

type File struct {
Expand Down Expand Up @@ -71,15 +75,44 @@ func parseDates(v interface{}) interface{} {
return v
}

func loadJS(input []byte) ([]byte, error) {
vm := goja.New()
_, err := vm.RunString(string(input))
if err != nil {
return nil, err
}

var fn func() map[string]interface{}
err = vm.ExportTo(vm.Get("data"), &fn)
if err != nil {
return nil, err
}

// Convert the response to a byte array
jsonData, err := json.Marshal(fn())
if err != nil {
return nil, err
}

return jsonData, nil
}

func (f *File) LoadFile() (jsonData *FileData, err error) {
var fileData []byte

switch f.Type {
case FileTypeJSON:
case FileTypeJSON, FileTypeJS:
fileData, err = os.ReadFile(f.Path)
if err != nil {
return nil, err
}

if f.Type == FileTypeJS {
fileData, err = loadJS(fileData)
if err != nil {
return nil, err
}
}
default:
return nil, fmt.Errorf("unknown file type: %s", f.Type)
}
Expand Down

0 comments on commit f1101f4

Please sign in to comment.