diff --git a/.golangci.yml b/.golangci.yml index 47a891e3..3d2a918a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -81,7 +81,7 @@ linters-settings: linters: disable-all: true enable: - - bodyclose + # - bodyclose ... some functions can't clsoe a body themselves because it will have to be read from another function - depguard - dogsled # - dupl diff --git a/env/spreadsheet.go b/env/spreadsheet.go index d568ba59..20b6f8b6 100644 --- a/env/spreadsheet.go +++ b/env/spreadsheet.go @@ -192,7 +192,8 @@ func (s Spreadsheet) Columns(ps *ProgramState, names []string) Object { row2 := make([]any, len(names)) for col := range idxs { if len(row.Values) > col { - row2[col] = row.Values[idxs[col]].(Object) + // row2[col] = row.Values[idxs[col]].(Object) + row2[col] = row.Values[idxs[col]] } } nspr.AddRow(SpreadsheetRow{row2, nspr}) diff --git a/evaldo/builtins.go b/evaldo/builtins.go index 62e52808..70cc414d 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -2713,6 +2713,14 @@ var builtins = map[string]*env.Builtin{ }, }, + "current-ctx!!": { // ** + Argsn: 0, + Doc: "Returns current context.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return ps.Ctx + }, + }, + "parent-ctx": { // ** Argsn: 0, Doc: "Returns parent context of the current context.", @@ -2954,7 +2962,7 @@ var builtins = map[string]*env.Builtin{ }, }, - "bind": { // ** + "bind!": { // ** Argsn: 2, Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { switch swCtx1 := arg0.(type) { @@ -2972,7 +2980,7 @@ var builtins = map[string]*env.Builtin{ }, }, - "unbind": { // ** + "unbind!": { // ** Argsn: 1, Doc: "Accepts a Context and unbinds it from it's parent Context.", Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { @@ -5241,6 +5249,14 @@ var builtins = map[string]*env.Builtin{ return MakeBuiltinError(ps, doc, "fn") } switch ctx := arg1.(type) { + case *env.RyeCtx: + switch body := arg2.(type) { + case env.Block: + return *env.NewFunctionC(args, body, ctx, false, true, doc) + default: + ps.ErrorFlag = true + return MakeArgError(ps, 3, []env.Type{env.BlockType}, "fnc") + } case env.RyeCtx: switch body := arg2.(type) { case env.Block: diff --git a/evaldo/builtins_io.go b/evaldo/builtins_io.go index 6ebcc483..0cf795a6 100755 --- a/evaldo/builtins_io.go +++ b/evaldo/builtins_io.go @@ -525,7 +525,7 @@ func __https_request__do(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, case env.Native: client := &http.Client{} resp, err := client.Do(req.Value.(*http.Request)) - defer resp.Body.Close() // TODO -- comment this and figure out goling bodyclose + // defer resp.Body.Close() // TODO -- comment this and figure out goling bodyclose if err != nil { return MakeBuiltinError(ps, err.Error(), "__https_request__do") } diff --git a/evaldo/builtins_json.go b/evaldo/builtins_json.go index e307396d..62d5d402 100644 --- a/evaldo/builtins_json.go +++ b/evaldo/builtins_json.go @@ -52,15 +52,19 @@ func RyeToJSON(res any) string { if v[0] == '[' && v[len(v)-1:] == "]" { return v } - return "\"" + v + "\"" + return "\"" + EscapeJson(v) + "\"" case env.String: - return "\"" + v.Value + "\"" + return "\"" + EscapeJson(v.Value) + "\"" case env.Integer: return strconv.Itoa(int(v.Value)) case env.Decimal: return strconv.Itoa(int(v.Value)) + case env.Dict: + return DictToJSON(v) case env.Spreadsheet: return SpreadsheetToJSON(v) + case env.SpreadsheetRow: + return SpreadsheetRowToJSON(v) case *env.Error: status := "" if v.Status != 0 { @@ -88,6 +92,78 @@ func RyeToJSON(res any) string { } } +func RyeToJSONLines(res any) string { + // fmt.Printf("Type: %T", res) + switch v := res.(type) { + case env.Spreadsheet: + return SpreadsheetToJSONLines(v) + case *env.Error: + status := "" + if v.Status != 0 { + status = "\"status\": " + RyeToJSON(v.Status) + } + var b strings.Builder + b.WriteString("{ " + status + ", \"message\": " + RyeToJSON(v.Message)) + if v.Parent != nil { + b.WriteString(", \"parent\": " + RyeToJSON(v.Parent)) + } + b.WriteString(", \"data\": { ") + for k, v := range v.Values { + switch ob := v.(type) { + case env.Object: + b.WriteString(" " + RyeToJSON(k) + ": " + RyeToJSON(ob) + ", ") + } + } + b.WriteString("} }") + return b.String() + case env.RyeCtx: + return "{ 'state': 'todo' }" + default: + return "\"not handeled\"" + // TODO-FIXME + } +} + +func EscapeJson(val string) string { + res := strings.ReplaceAll(val, "\"", "\\\"") + return res +} + +// Inspect returns a string representation of the Integer. +func DictToJSON(dict env.Dict) string { + var bu strings.Builder + bu.WriteString("{") + i := 0 + for key, val := range dict.Data { + if i > 0 { + bu.WriteString(", ") + } + bu.WriteString(RyeToJSON(key)) + bu.WriteString(": ") + bu.WriteString(RyeToJSON(val)) + i = i + 1 + } + bu.WriteString("} ") + return bu.String() +} + +// Inspect returns a string representation of the Integer. +func SpreadsheetRowToJSON(row env.SpreadsheetRow) string { + var bu strings.Builder + bu.WriteString("{") + for i, val := range row.Values { + if i > 0 { + bu.WriteString(", ") + } + bu.WriteString("\"") + bu.WriteString(row.Uplink.Cols[i]) + bu.WriteString("\": ") + bu.WriteString(RyeToJSON(val)) + } + bu.WriteString("} ") + return bu.String() +} + // Inspect returns a string representation of the Integer. func SpreadsheetToJSON(s env.Spreadsheet) string { //fmt.Println("IN TO Html") @@ -98,23 +174,25 @@ func SpreadsheetToJSON(s env.Spreadsheet) string { if i > 0 { bu.WriteString(", ") } - bu.WriteString("{") - for i, val := range row.Values { - if i > 0 { - bu.WriteString(", ") - } - bu.WriteString("\"") - bu.WriteString(s.Cols[i]) - bu.WriteString("\": ") - bu.WriteString(RyeToJSON(val)) - } - bu.WriteString("} ") + bu.WriteString(SpreadsheetRowToJSON(row)) } bu.WriteString("]") //fmt.Println(bu.String()) return bu.String() } +func SpreadsheetToJSONLines(s env.Spreadsheet) string { + //fmt.Println("IN TO Html") + var bu strings.Builder + //fmt.Println(len(s.Rows)) + for _, row := range s.Rows { + bu.WriteString(SpreadsheetRowToJSON(row)) + bu.WriteString("\n") + } + //fmt.Println(bu.String()) + return bu.String() +} + // { [ .print ] } // { { _ [ .print ] { _ [ .print2 ";" ] } } @@ -149,4 +227,11 @@ var Builtins_json = map[string]*env.Builtin{ return *env.NewString(RyeToJSON(arg0)) }, }, + "to-json\\lines": { + Argsn: 1, + Doc: "Takes a Rye value and returns it encoded into JSON.", + Fn: func(es *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(RyeToJSONLines(arg0)) + }, + }, } diff --git a/evaldo/builtins_spreadsheet.go b/evaldo/builtins_spreadsheet.go index 5feafdbc..58c78f8a 100644 --- a/evaldo/builtins_spreadsheet.go +++ b/evaldo/builtins_spreadsheet.go @@ -277,6 +277,12 @@ var Builtins_spreadsheet = map[string]*env.Builtin{ for i, v := range row.Values { var sv string switch tv := v.(type) { + case string: + sv = tv + case int64: + sv = strconv.Itoa(int(tv)) + case float64: + sv = strconv.FormatFloat(tv, 'f', -1, 64) case env.String: sv = tv.Value case env.Integer: @@ -1169,9 +1175,27 @@ func LeftJoin(ps *env.ProgramState, s1 env.Spreadsheet, s2 env.Spreadsheet, col1 if err != nil { return MakeError(ps, fmt.Sprintf("Couldn't retrieve value at row %d (%s)", j, err)) } - if val1.(env.Object).Equal(val2.(env.Object)) { - s2RowId = j - break + val1o, ok := val1.(env.Object) + if ok { + val2o, ok1 := val2.(env.Object) + if ok1 { + if val1o.Equal(val2o) { + s2RowId = j + break + } + } else { + if env.RyeToRaw(val1o) == val2 { + s2RowId = j + break + } + } + } + val1s, ok := val1.(string) + if ok { + if val1s == val2.(string) { + s2RowId = j + break + } } } } diff --git a/examples/examples-wip/full-text-search/minimal.nogo b/examples/examples-wip/full-text-search/minimal.nogo deleted file mode 100644 index f174572c..00000000 --- a/examples/examples-wip/full-text-search/minimal.nogo +++ /dev/null @@ -1,18 +0,0 @@ -// Go example - -import "github.com/blevesearch/bleve/v2" - -func main() { - - mapping := bleve.NewIndexMapping() - index, err := bleve.New("example.bleve", mapping) - - err = index.Index("elephant and a cat", "A mouse scared an elephant. A cat caught an mouse.") - err = index.Index("mouse and dog", "Cat was hunting for a mouse. A dog chased it away.") - err = index.Index("elephant and a dog", "Elephant looked at the dog. The dog looked at the cat.") - - query := bleve.NewMatchQuery("dog") - search := bleve.NewSearchRequest(query) - searchResults, err := index.Search(search) - fmt.Println(searchResults) -} diff --git a/examples/examples-wip/full-text-search/minimal.rye b/examples/examples-wip/full-text-search/minimal.rye deleted file mode 100644 index 35b88670..00000000 --- a/examples/examples-wip/full-text-search/minimal.rye +++ /dev/null @@ -1,17 +0,0 @@ - -; Rye example - -rye .needs { bleve } - -%minimal6.bleve :db -|open-bleve |fix { new-bleve-index-mapping .new-bleve db } :idx -;|index "elephant and a cat" "A mouse scared an elephant. A cat caught an mouse." -;|index "mouse and dog" "Cat was hunting for a mouse. A dog chased it away." -;|index "elephant and a dog" "Elephant looked at the dog. The dog looked at the cat." -|index "faq1" ${ "question": "Is cebelca.biz really free?", "answer": "Yes, it really is free", "keywords": "billing,free,expenses,newusers" }$ -|index "faq2" ${ "question": "Can I fiscalize invoices with cebelca.biz?", "answer": "Yes, you can fiscalize invoices with online on mobile POS version.", "keywords": "fiscalisation,invoices" }$ - -bleve-search: fn { phrase idx } { new-match-query phrase |new-search-request |search idx |print } - -enter-console "search" - diff --git a/examples/examples-wip/full-text-search/with-mapping.rye b/examples/examples-wip/full-text-search/with-mapping.rye deleted file mode 100644 index 65254efb..00000000 --- a/examples/examples-wip/full-text-search/with-mapping.rye +++ /dev/null @@ -1,37 +0,0 @@ - -; Rye example of bleve full-text-search - a simple search console - -rye .needs { bleve } - -private\ "console will run inside this context" { - - private\ "opens or creates a bleve index and returns it" { - %demo8.bleve :db |open-bleve - |fix { - new-bleve-document-mapping :fqm - new-bleve-text-field-mapping :tfm - - new-bleve-index-mapping :im - |add-document-mapping "faq" fqm - |add-field-mapping-at "question" tfm - |add-field-mapping-at "answer" tfm - |add-field-mapping-at "keywords" tfm - - im .new-bleve db - } - } :idx - ; we can index a JSON string - |index "presale1" ${ "q": "Is InvoiceFox really free?", "a": "Yes, our Mini plans are absolutely free.", "kw": "billing,free,plans" }$ - |index "presale2" ${ "q": "Can I quit at any time?", "a": "Yes, and you can change packages at any time.", "kw": "billing,plans" }$ - |index "integra1" ${ "q": "Can I integrate with Woocommerce? ", "a": "Yes, we have open source WP plugin on github.", "kw": "webshop,integration" }$ - |index "integra2" ${ "q": "Can I integrate inventory and mobile POS?", "a": "Yes, mobile POS can affect and check the inventory.", "kw": "pos,integration" }$ - - ; define two functions that we will be able to call in console - search: fn1 { .new-match-query |new-search-request :sr Search sr idx } - index: fn { id data } { Index idx id data } - - ; enter the Rye console - enter-console "you can index and search here" - -} - diff --git a/examples/examples-wip/full-text-search/with-mapping2.rye b/examples/examples-wip/full-text-search/with-mapping2.rye deleted file mode 100644 index 8bc2e46b..00000000 --- a/examples/examples-wip/full-text-search/with-mapping2.rye +++ /dev/null @@ -1,36 +0,0 @@ - - - -; Rye example of bleve full-text-search - a simple search console - -rye .needs { bleve } - -private\ "console will run inside this context" { - - private\ "opens or creates a bleve index and returns it" { - %demo12.bleve :db |open-bleve - |fix { - new-bleve-document-mapping :fqm - new-bleve-text-field-mapping :tfm - - new-bleve-index-mapping :im - |add-document-mapping "faq" fqm - |add-field-mapping-at "q" tfm - |add-field-mapping-at "a" tfm - - im .new-bleve db - } - } :idx - ; we can index a JSON string - |index "qa1" ${ "q": "Where is Sevnica?", "a": "It's in Slovenia" }$ - |index "qa2" ${ "q": "What is Slovenia?", "a": "It's a country" }$ - - ; define two functions that we will be able to call in console - search: fn1 { .new-match-query |new-search-request :sr Search sr idx } - index: fn { id data } { Index idx id data } - - ; enter the Rye console - enter-console "you can index and search here" - -} - diff --git a/util/ryel.rye b/util/ryel.rye index 9745a898..4099556b 100644 --- a/util/ryel.rye +++ b/util/ryel.rye @@ -29,8 +29,8 @@ build-ryel: fn { tags } { cp-embed: either tags = "embed_main" { "cp main.rye %RYE_HOME/buildtemp/. ; " } { "" } - command: join { cp-embed $ a=%PWD ; cd %RYE_HOME ; go build -tags "$ + modules + " " + tags $" -o "%a/ryel" ; cd "%a"$ } ; $ delimiter for string is just temporary ... ' conflicts with lit-words and Rebols { } with blocks so this question is open so far - command .replace "%" "$" :command + command: join { cp-embed ` a=%PWD ; cd %RYE_HOME ; go build -tags "` + modules + " " + tags `" -o "%a/ryel" ; cd "%a"` } ; ` delimiter for string is just temporary ... ' conflicts with lit-words and Rebols { } with blocks so this question is open so far + command .replace "%" "$" ::command ; print "Build command: " + command cmd command }