added custom goja field mapper to handle all caps identifiers and allowed errors unwrapping
This commit is contained in:
+39
-1
@@ -2,6 +2,9 @@ package jsvm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/pocketbase/dbx"
|
||||
@@ -14,7 +17,7 @@ import (
|
||||
|
||||
func NewBaseVM(app core.App) *goja.Runtime {
|
||||
vm := goja.New()
|
||||
vm.SetFieldNameMapper(goja.UncapFieldNameMapper())
|
||||
vm.SetFieldNameMapper(fieldMapper{})
|
||||
vm.Set("$app", app)
|
||||
|
||||
baseBind(vm)
|
||||
@@ -137,3 +140,38 @@ func apisBind(vm *goja.Runtime) {
|
||||
obj.Set("enrichRecord", apis.EnrichRecord)
|
||||
obj.Set("enrichRecords", apis.EnrichRecords)
|
||||
}
|
||||
|
||||
// fieldMapper provides custom mapping between Go and JavaScript property names.
|
||||
//
|
||||
// It is similar to the builtin "uncapFieldNameMapper" but also converts
|
||||
// all uppercase identifiers to their lowercase equivalent (eg. "GET" -> "get").
|
||||
type fieldMapper struct {
|
||||
}
|
||||
|
||||
// FieldName implements the [FieldNameMapper.FieldName] interface method.
|
||||
func (u fieldMapper) FieldName(_ reflect.Type, f reflect.StructField) string {
|
||||
return convertGoToJSName(f.Name)
|
||||
}
|
||||
|
||||
// MethodName implements the [FieldNameMapper.MethodName] interface method.
|
||||
func (u fieldMapper) MethodName(_ reflect.Type, m reflect.Method) string {
|
||||
return convertGoToJSName(m.Name)
|
||||
}
|
||||
|
||||
func convertGoToJSName(name string) string {
|
||||
allUppercase := true
|
||||
for _, c := range name {
|
||||
if !unicode.IsUpper(c) {
|
||||
allUppercase = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// eg. "JSON" -> "json"
|
||||
if allUppercase {
|
||||
return strings.ToLower(name)
|
||||
}
|
||||
|
||||
// eg. "GetField" -> "getField"
|
||||
return strings.ToLower(name[0:1]) + name[1:]
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func (p *plugin) afterCollectionChange() func(*core.ModelEvent) error {
|
||||
template, templateErr = p.goDiffTemplate(new, old)
|
||||
}
|
||||
if templateErr != nil {
|
||||
return fmt.Errorf("failed to resolve template: %v", templateErr)
|
||||
return fmt.Errorf("failed to resolve template: %w", templateErr)
|
||||
}
|
||||
|
||||
var action string
|
||||
@@ -61,11 +61,11 @@ func (p *plugin) afterCollectionChange() func(*core.ModelEvent) error {
|
||||
|
||||
// ensure that the local migrations dir exist
|
||||
if err := os.MkdirAll(p.options.Dir, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("failed to create migration dir: %v", err)
|
||||
return fmt.Errorf("failed to create migration dir: %w", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(fileDest, []byte(template), 0644); err != nil {
|
||||
return fmt.Errorf("failed to save automigrate file: %v", err)
|
||||
return fmt.Errorf("failed to save automigrate file: %w", err)
|
||||
}
|
||||
|
||||
p.refreshCachedCollections()
|
||||
|
||||
@@ -35,7 +35,7 @@ func (p *plugin) jsBlankTemplate() (string, error) {
|
||||
func (p *plugin) jsSnapshotTemplate(collections []*models.Collection) (string, error) {
|
||||
jsonData, err := marhshalWithoutEscape(collections, " ", " ")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to serialize collections list: %v", err)
|
||||
return "", fmt.Errorf("failed to serialize collections list: %w", err)
|
||||
}
|
||||
|
||||
const template = `migrate((db) => {
|
||||
@@ -55,7 +55,7 @@ func (p *plugin) jsSnapshotTemplate(collections []*models.Collection) (string, e
|
||||
func (p *plugin) jsCreateTemplate(collection *models.Collection) (string, error) {
|
||||
jsonData, err := marhshalWithoutEscape(collection, " ", " ")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to serialize collections list: %v", err)
|
||||
return "", fmt.Errorf("failed to serialize collections list: %w", err)
|
||||
}
|
||||
|
||||
const template = `migrate((db) => {
|
||||
@@ -76,7 +76,7 @@ func (p *plugin) jsCreateTemplate(collection *models.Collection) (string, error)
|
||||
func (p *plugin) jsDeleteTemplate(collection *models.Collection) (string, error) {
|
||||
jsonData, err := marhshalWithoutEscape(collection, " ", " ")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to serialize collections list: %v", err)
|
||||
return "", fmt.Errorf("failed to serialize collections list: %w", err)
|
||||
}
|
||||
|
||||
const template = `migrate((db) => {
|
||||
@@ -329,7 +329,7 @@ func init() {
|
||||
func (p *plugin) goSnapshotTemplate(collections []*models.Collection) (string, error) {
|
||||
jsonData, err := marhshalWithoutEscape(collections, "\t\t", "\t")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to serialize collections list: %v", err)
|
||||
return "", fmt.Errorf("failed to serialize collections list: %w", err)
|
||||
}
|
||||
|
||||
const template = `package %s
|
||||
@@ -368,7 +368,7 @@ func init() {
|
||||
func (p *plugin) goCreateTemplate(collection *models.Collection) (string, error) {
|
||||
jsonData, err := marhshalWithoutEscape(collection, "\t\t", "\t")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to serialize collections list: %v", err)
|
||||
return "", fmt.Errorf("failed to serialize collections list: %w", err)
|
||||
}
|
||||
|
||||
const template = `package %s
|
||||
@@ -416,7 +416,7 @@ func init() {
|
||||
func (p *plugin) goDeleteTemplate(collection *models.Collection) (string, error) {
|
||||
jsonData, err := marhshalWithoutEscape(collection, "\t\t", "\t")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to serialize collections list: %v", err)
|
||||
return "", fmt.Errorf("failed to serialize collections list: %w", err)
|
||||
}
|
||||
|
||||
const template = `package %s
|
||||
|
||||
Reference in New Issue
Block a user