Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modwords - improvements to spreadsheets, bleve search and json #218

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion env/spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
20 changes: 18 additions & 2 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion evaldo/builtins_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
111 changes: 98 additions & 13 deletions evaldo/builtins_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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()
}

// { <person> [ .print ] }
// { <person> { _ [ .print ] <name> <surname> <age> { _ [ .print2 ";" ] } }

Expand Down Expand Up @@ -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))
},
},
}
30 changes: 27 additions & 3 deletions evaldo/builtins_spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
}
}
}
Expand Down
18 changes: 0 additions & 18 deletions examples/examples-wip/full-text-search/minimal.nogo

This file was deleted.

17 changes: 0 additions & 17 deletions examples/examples-wip/full-text-search/minimal.rye

This file was deleted.

37 changes: 0 additions & 37 deletions examples/examples-wip/full-text-search/with-mapping.rye

This file was deleted.

36 changes: 0 additions & 36 deletions examples/examples-wip/full-text-search/with-mapping2.rye

This file was deleted.

4 changes: 2 additions & 2 deletions util/ryel.rye
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down