mirror of
https://github.com/stashapp/stash.git
synced 2025-12-06 08:26:00 +01:00
Fix inconsistent field names in javascript plugin hooks (#4869)
This commit is contained in:
parent
4794a1d453
commit
98d210f7f9
2 changed files with 23 additions and 1 deletions
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/dop251/goja"
|
"github.com/dop251/goja"
|
||||||
)
|
)
|
||||||
|
|
@ -16,8 +17,27 @@ type VM struct {
|
||||||
GQLHandler http.Handler
|
GQLHandler http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// optionalFieldNameMapper wraps a goja.FieldNameMapper and returns the field name if the wrapped mapper returns an empty string.
|
||||||
|
type optionalFieldNameMapper struct {
|
||||||
|
mapper goja.FieldNameMapper
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tfm optionalFieldNameMapper) FieldName(t reflect.Type, f reflect.StructField) string {
|
||||||
|
if ret := tfm.mapper.FieldName(t, f); ret != "" {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tfm optionalFieldNameMapper) MethodName(t reflect.Type, m reflect.Method) string {
|
||||||
|
return tfm.mapper.MethodName(t, m)
|
||||||
|
}
|
||||||
|
|
||||||
func NewVM() *VM {
|
func NewVM() *VM {
|
||||||
return &VM{Runtime: goja.New()}
|
r := goja.New()
|
||||||
|
r.SetFieldNameMapper(optionalFieldNameMapper{goja.TagFieldNameMapper("json", true)})
|
||||||
|
return &VM{Runtime: r}
|
||||||
}
|
}
|
||||||
|
|
||||||
type APIAdder interface {
|
type APIAdder interface {
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,8 @@ func (t *jsPluginTask) makeOutput(o goja.Value) {
|
||||||
func (t *jsPluginTask) initVM() error {
|
func (t *jsPluginTask) initVM() error {
|
||||||
// converting the Args field to map[string]interface{} is required, otherwise
|
// converting the Args field to map[string]interface{} is required, otherwise
|
||||||
// it gets converted to an empty object
|
// it gets converted to an empty object
|
||||||
|
// ideally this should have included json tags with the correct casing but changing
|
||||||
|
// it now will result in a breaking change
|
||||||
type pluginInput struct {
|
type pluginInput struct {
|
||||||
// Server details to connect to the stash server.
|
// Server details to connect to the stash server.
|
||||||
ServerConnection common.StashServerConnection
|
ServerConnection common.StashServerConnection
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue