Skip to content

Commit

Permalink
Merge branch 'beta'
Browse files Browse the repository at this point in the history
  • Loading branch information
gentee committed Nov 12, 2020
2 parents 301ccc5 + 3d2290b commit 1cfcae5
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 236 deletions.
465 changes: 262 additions & 203 deletions assets.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const (

var (
cfg = Config{
Version: Version,
Version: GetVersion(),
Mode: ModeDefault,
Log: LogConfig{
Mode: logModeFile,
Expand Down
12 changes: 11 additions & 1 deletion const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package main

const (
// Version of the application
Version = "1.4.0+1"
Version = "1.5.0"
// DefPort is the default web-server port
DefPort = 3234
// DefTheme is the default web-server theme
Expand Down Expand Up @@ -46,3 +46,13 @@ type AppInfo struct {
Lang string
Issue string
}

var VerType string

func GetVersion() string {
ret := Version
if VerType == `beta` {
ret += `b`
}
return ret + `+1`
}
103 changes: 82 additions & 21 deletions gensource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"hash/crc64"
"reflect"
"strconv"
"strings"

es "eonza/script"
Expand Down Expand Up @@ -195,7 +196,7 @@ func (src *Source) ScriptValues(script *Script, node scriptTree) ([]Param, []Par
ptype, value = src.getTypeValue(script, par, value)
switch par.Type {
case es.PTextarea, es.PSingleText, es.PNumber:
if isEmpty && par.Options.Required {
if isEmpty && par.Options.Required && len(node.Name) != 0 {
return nil, nil, errField(par.Title)
}
case es.PList:
Expand Down Expand Up @@ -418,42 +419,102 @@ func ValToStr(input string) string {
}

func GenSource(script *Script, header *es.Header) (string, error) {
var params string
var params []string
src := &Source{
Linked: make(map[string]bool),
CRCTable: crc64.MakeTable(crc64.ISO),
HashStrings: make(map[uint64]int),
Header: header,
}
values, optvalues, err := src.ScriptValues(script, scriptTree{})
if err != nil {
return ``, err
}
for _, par := range append(values, optvalues...) {
val := par.Value
if par.Type == `str` {
val = ValToStr(val)
}
params += fmt.Sprintf("%s %s = %s\r\n", par.Type, par.Name, val)
}
level := storage.Settings.LogLevel
if script.Settings.LogLevel < es.LOG_INHERIT {
level = script.Settings.LogLevel
}
params += fmt.Sprintf("SetLogLevel(%d)\r\ninit()\r\n", level)
code := strings.TrimSpace(strings.ReplaceAll(script.Code, `%body%`, ``))
if len(code) > 0 {
code += "\r\n"
}
params = append(params, fmt.Sprintf("SetLogLevel(%d)\r\ninit()", level))

if predef, err := src.Predefined(script); err != nil {
return ``, err
} else {
code = predef + code
} else if len(strings.TrimSpace(predef)) > 0 {
params = append(params, strings.TrimSpace(predef))
}
var (
code string
jsonForm []es.FormParam
)

// Define form for parameters
for i, par := range script.Params {
var (
pval, setvar string
)
pval = par.Options.Initial
if len(pval) == 0 {
pval = par.Options.Default
}

switch par.Type {
case es.PList:
params = append(params, fmt.Sprintf(`arr.obj %s%d`, par.Name, i))
setvar = fmt.Sprintf(`SetVar("%s", obj(%[1]s%d))`, par.Name, i)
default:
setvar = fmt.Sprintf(`SetVar("%s", %s)`, par.Name, src.Value(pval))
}
if par.Type != es.PList && !par.Options.Optional {
parOpt, err := json.Marshal(par.Options)
if err != nil {
return ``, err
}
jsonForm = append(jsonForm, es.FormParam{
Var: par.Name,
Text: es.ReplaceVars(par.Title, script.Langs[src.Header.Lang], &langRes[langsId[src.Header.Lang]]),
Type: strconv.FormatInt(int64(par.Type), 10),
Options: string(parOpt),
})
}
params = append(params, setvar)
}
if len(jsonForm) > 0 {
outForm, err := json.Marshal(jsonForm)
if err != nil {
return ``, err
}
params = append(params, fmt.Sprintf("initcmd(`form`, %s)\r\nForm( %[1]s )", src.Value(string(outForm))))
}
for _, par := range script.Params {
var ptype string
switch par.Type {
case es.PCheckbox:
ptype = `bool`
case es.PSelect:
if len(par.Options.Type) > 0 {
ptype = par.Options.Type
}
case es.PNumber:
ptype = `int`
case es.PList:
ptype = `obj`
}
switch ptype {
case `bool`:
params = append(params, fmt.Sprintf(`bool %s = GetVarBool("%[1]s")`, par.Name))
case `int`:
params = append(params, fmt.Sprintf(`int %s = GetVarInt("%[1]s")`, par.Name))
case `obj`:
params = append(params, fmt.Sprintf(`obj %s = GetVarObj("%[1]s")`, par.Name))
default:
params = append(params, fmt.Sprintf(`str %s = GetVar("%[1]s")`, par.Name))
}
}
code = strings.Join(params, "\r\n")
body, err := src.Tree(script.Tree)
if err != nil {
return ``, err
}
if strings.Contains(script.Code, `%body%`) {
body = strings.TrimSpace(strings.ReplaceAll(script.Code, `%body%`, body))
} else {
body = script.Code + "\r\n" + body
}
var constStr string
if len(src.Strings) > 0 {
constStr = "const {\r\n"
Expand All @@ -465,6 +526,6 @@ func GenSource(script *Script, header *es.Header) (string, error) {
constStr += `const IOTA { LOG_DISABLE
LOG_ERROR LOG_WARN LOG_FORM LOG_INFO LOG_DEBUG }
`
return fmt.Sprintf("%s%s\r\nrun {\r\n%s%s%s\r\ndeinit()}", constStr, src.Funcs, params,
return fmt.Sprintf("%s%s\r\nrun {\r\n%s\r\n%s\r\ndeinit()}", constStr, src.Funcs,
code, body), nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/alecthomas/chroma v0.8.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gentee/gentee v1.16.0
github.com/gentee/gentee v1.17.0
github.com/gorilla/websocket v1.4.2
github.com/kataras/golog v0.1.5
github.com/labstack/echo/v4 v4.1.17
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/gentee/gentee v1.15.1 h1:S9p7S9ZM/1cUxtvfar8olPz6ZoL0/CEbbqPCy8M4G8E=
github.com/gentee/gentee v1.15.1/go.mod h1:XeyqrgfEuwpfJM/YO2A1uUKbnqUFKnc0qcq6NOX1ggk=
github.com/gentee/gentee v1.16.0 h1:qhif/bGyJrOaAqeGaP8e8969otOmgfXSYZ3msw+Fmqs=
github.com/gentee/gentee v1.16.0/go.mod h1:XeyqrgfEuwpfJM/YO2A1uUKbnqUFKnc0qcq6NOX1ggk=
github.com/gentee/gentee v1.17.0 h1:NLTouUGQM2Rs1eeDHCl9m+GHjUbYMfje3WcKY+lvhKA=
github.com/gentee/gentee v1.17.0/go.mod h1:XeyqrgfEuwpfJM/YO2A1uUKbnqUFKnc0qcq6NOX1ggk=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/gorilla/csrf v1.6.0/go.mod h1:7tSf8kmjNYr7IWDCYhd3U8Ck34iQ/Yw5CJu7bAkHEGI=
Expand Down
3 changes: 2 additions & 1 deletion make.g
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
run {
str env = $ go env
$GOPATH = RegExp(env, `GOPATH="?([^"|\n|\r]*)`)
str vertype = `beta`

$ /home/ak/go/bin/esc -ignore "\.git|LICENSE|README.md" -o assets.go ../eonza-assets
// $ go install -ldflags "-s -w" -tags "eonza standard"
$ go install -ldflags "-s -w"
$ go install -ldflags "-s -w -X main.VerType=%{vertype}"
$ cp ${GOPATH}/bin/eonza /home/ak/app/eonza-dev/eonza
$ cp ${GOPATH}/bin/eonza /home/ak/app/eonza/eonza
$ /home/ak/app/eonza-dev/eonza
Expand Down
2 changes: 1 addition & 1 deletion render.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func RenderPage(c echo.Context, url string) (string, error) {
data = renderScript
} else {
render.App = appInfo
render.Version = Version
render.Version = GetVersion()
render.Develop = cfg.develop
render.Playground = cfg.playground
render.Lang = GetLangCode(c.(*Auth).User)
Expand Down
18 changes: 15 additions & 3 deletions script/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ type ScriptParam struct {
Options ScriptOptions `json:"options,omitempty" yaml:"options,omitempty"`
}

type FormParam struct {
Var string `json:"var,omitempty"`
Text string `json:"text,omitempty"`
Value string `json:"value,omitempty"`
Type string `json:"type"`
Options string `json:"options,omitempty"`
}

type ScriptOptions struct {
Initial string `json:"initial,omitempty" yaml:"initial,omitempty"`
Default string `json:"default,omitempty" yaml:"default,omitempty"`
Expand Down Expand Up @@ -112,6 +120,7 @@ var (
{Prototype: `Condition(str,str) bool`, Object: Condition},
{Prototype: `File(str) str`, Object: FileLoad},
{Prototype: `Form(str)`, Object: Form},
{Prototype: `IsVarObj(str) bool`, Object: IsVarObj},
{Prototype: `IsVar(str) bool`, Object: IsVar},
{Prototype: `LogOutput(int,str)`, Object: LogOutput},
{Prototype: `Macro(str) str`, Object: Macro},
Expand Down Expand Up @@ -264,7 +273,7 @@ func FileLoad(rt *vm.Runtime, fname string) (ret string, err error) {
}

func GetVar(name string) (ret string, err error) {
if IsVar(name) {
if IsVar(name) != 0 {
id := len(dataScript.Vars) - 1
ret, err = Macro(dataScript.Vars[id][name])
}
Expand Down Expand Up @@ -321,11 +330,14 @@ func InitCmd(name string, pars ...interface{}) bool {
return true
}

func IsVar(key string) bool {
func IsVar(key string) int64 {
dataScript.Mutex.Lock()
defer dataScript.Mutex.Unlock()
_, ret := dataScript.Vars[len(dataScript.Vars)-1][key]
return ret
if ret {
return 1
}
return 0
}

func loadForm(data string, form *[]map[string]interface{}) error {
Expand Down
18 changes: 14 additions & 4 deletions script/varobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
)

func ObjToStr(key string) (string, bool) {
if v, ok := dataScript.ObjVars[len(dataScript.ObjVars)-1].Load(key); ok {
switch vm.Type(v.(*core.Obj)) {
case `int`, `float`, `str`, `bool`:
return fmt.Sprint(v.(*core.Obj).Data), true
if len(dataScript.ObjVars) > 0 {
if v, ok := dataScript.ObjVars[len(dataScript.ObjVars)-1].Load(key); ok {
switch vm.Type(v.(*core.Obj)) {
case `int`, `float`, `str`, `bool`:
return fmt.Sprint(v.(*core.Obj).Data), true
}
}
}
return ``, false
Expand Down Expand Up @@ -109,6 +111,14 @@ func GetVarObj(name string) (*core.Obj, error) {
return val.(*core.Obj), nil
}

func IsVarObj(name string) int64 {
_, ok := dataScript.ObjVars[len(dataScript.ObjVars)-1].Load(name)
if ok {
return 1
}
return 0
}

func setRawVarObj(shift int, name string, value *core.Obj) error {
off := len(dataScript.ObjVars) - 1 - shift
if off < 0 {
Expand Down
21 changes: 21 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"eonza/lib"
"eonza/script"
es "eonza/script"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -513,6 +514,26 @@ func formHandle(c echo.Context) error {
return jsonError(c, err)
}
if len(formData) > 0 && formData[0].ID == form.FormID {
var formParams []es.FormParam
if err = json.Unmarshal([]byte(formData[0].Data), &formParams); err != nil {
return jsonError(c, err)
}
for _, item := range formParams {
var options es.ScriptOptions
if len(item.Options) == 0 {
continue
}
ptype, _ := strconv.ParseInt(item.Type, 10, 62)
switch es.ParamType(ptype) {
case es.PNumber, es.PSingleText, es.PTextarea:
if err = json.Unmarshal([]byte(item.Options), &options); err != nil {
return jsonError(c, err)
}
if options.Required && len(fmt.Sprint(form.Values[item.Var])) == 0 {
return jsonError(c, fmt.Errorf(Lang(GetLangId(nil), "errreq", item.Text)))
}
}
}
for key, val := range form.Values {
script.SetVar(key, fmt.Sprint(val))
}
Expand Down

0 comments on commit 1cfcae5

Please sign in to comment.