Skip to content

Commit

Permalink
Delete more extraneous stuff from public gopackags
Browse files Browse the repository at this point in the history
Deleted complex data types (struct, map, array), and complex
native types (mutex, waitgroup)
  • Loading branch information
tucats committed Feb 8, 2023
1 parent dfee472 commit 0b27e11
Show file tree
Hide file tree
Showing 76 changed files with 89 additions and 9,505 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ They allow for the definition of the command line grammar (including type checki
values, missing arguments, etc) and a defined action routine called when a subcommand is
processed successfully.

A simple command line tool defines a grammar for the commands and subcommands, and their
A simple command line tool defines a grammar for the commands and subcommands, and their
options. It then calls the app package Run() method which handles parsing and execution
control from then on.

Expand Down
31 changes: 0 additions & 31 deletions app-cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ package app
import (
"fmt"
"os"
"runtime"
"strings"

"github.com/tucats/gopackages/app-cli/cli"
"github.com/tucats/gopackages/app-cli/settings"
"github.com/tucats/gopackages/app-cli/ui"
"github.com/tucats/gopackages/defs"
"github.com/tucats/gopackages/errors"
"github.com/tucats/gopackages/expressions/data"
"github.com/tucats/gopackages/expressions/symbols"
)

Expand Down Expand Up @@ -135,35 +133,6 @@ func (app *App) Run(grammar []cli.Option, args []string) error {
Action: app.Action,
}

// Create the platform definition symbols
platformType := data.StructureType(
data.Field{
Name: "os",
Type: data.StringType,
},
data.Field{
Name: "arch",
Type: data.StringType,
},
data.Field{
Name: "go",
Type: data.StringType,
},
data.Field{
Name: "cpus",
Type: data.IntType,
},
)

platform := data.NewStruct(platformType)
_ = platform.Set("go", runtime.Version())
_ = platform.Set("os", runtime.GOOS)
_ = platform.Set("arch", runtime.GOARCH)
_ = platform.Set("cpus", runtime.NumCPU())
platform.SetReadonly(true)
_ = symbols.RootSymbolTable.SetWithAttributes(defs.PlatformVariable, platform,
symbols.SymbolAttribute{Readonly: true})

if err := SetDefaultLoggers(); err != nil {
return err
}
Expand Down
40 changes: 0 additions & 40 deletions expressions/builtins/append.go

This file was deleted.

109 changes: 4 additions & 105 deletions expressions/builtins/cast.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package builtins

import (
"math"
"strings"

"github.com/tucats/gopackages/errors"
"github.com/tucats/gopackages/expressions/data"
"github.com/tucats/gopackages/expressions/symbols"
Expand All @@ -16,121 +13,23 @@ import (
func Cast(s *symbols.SymbolTable, args []interface{}) (interface{}, error) {
// Target t is the last parameter
t := data.TypeOf(args[len(args)-1])

source := args[0]
if len(args) > 2 {
source = data.NewArrayFromArray(data.InterfaceType, args[:len(args)-1])
}
source := args[len(args)-1]

if t.IsString() {

// If the source is a []byte type, we can just fetch the bytes and do a direct convesion.
// If the source is a []int type, we can convert each integer to a rune and add it to a
// string builder. Otherwise, just format it as a string value.
if actual, ok := source.(*data.Array); ok && actual != nil && actual.Type().IsType(data.ByteType) {
b := actual.GetBytes()

return string(b), nil
} else if actual, ok := source.(*data.Array); ok && actual != nil && actual.Type().IsIntegerType() {
r := strings.Builder{}
for i := 0; i < actual.Len(); i++ {
ch, _ := actual.Get(i)
r.WriteRune(rune(data.Int(ch) & math.MaxInt32))
}
return data.FormatUnquoted(source), nil

return r.String(), nil
} else {
return data.FormatUnquoted(source), nil
}
}

switch actual := source.(type) {
// Conversion of one array type to another
case *data.Array:
if t.IsType(actual.Type()) {
return actual, nil
}

if t.IsString() && (actual.Type().IsIntegerType() || actual.Type().IsInterface()) {
r := strings.Builder{}

for i := 0; i < actual.Len(); i++ {
ch, _ := actual.Get(i)
r.WriteRune(data.Int32(ch) & math.MaxInt32)
}

return r.String(), nil
}

elementKind := *t.BaseType()
r := data.NewArray(t.BaseType(), actual.Len())

for i := 0; i < actual.Len(); i++ {
v, _ := actual.Get(i)

switch elementKind.Kind() {
case data.BoolKind:
_ = r.Set(i, data.Bool(v))

case data.ByteKind:
_ = r.Set(i, data.Byte(v))

case data.Int32Kind:
_ = r.Set(i, data.Int32(v))

case data.IntKind:
_ = r.Set(i, data.Int(v))

case data.Int64Kind:
_ = r.Set(i, data.Int64(v))

case data.Float32Kind:
_ = r.Set(i, data.Float32(v))

case data.Float64Kind:
_ = r.Set(i, data.Float64(v))

case data.StringKind:
_ = r.Set(i, data.String(v))

default:
return nil, errors.ErrInvalidType.Context(data.TypeOf(v).String())
}
}

return r, nil
switch source.(type) {

case string:
if t.IsType(data.ArrayType(data.IntType)) {
r := data.NewArray(data.IntType, 0)

for _, rune := range actual {
r.Append(int(rune))
}

return r, nil
}

if t.IsType(data.ArrayType(data.ByteType)) {
r := data.NewArray(data.ByteType, 0)

for i := 0; i < len(actual); i++ {
r.Append(actual[i])
}

return r, nil
}

return data.Coerce(source, data.InstanceOfType(t)), nil

default:
if t.IsArray() {
r := data.NewArray(t.BaseType(), 1)
value := data.Coerce(source, data.InstanceOfType(t.BaseType()))
_ = r.Set(0, value)

return r, nil
}

v := data.Coerce(source, data.InstanceOfType(t))
if v != nil {
return v, nil
Expand Down
29 changes: 0 additions & 29 deletions expressions/builtins/close.go

This file was deleted.

37 changes: 0 additions & 37 deletions expressions/builtins/copy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package builtins

import "github.com/tucats/gopackages/expressions/data"

// MaxDeepCopyDepth specifies the maximum depth that a recursive
// copy will go before failing. Setting this too small will
// prevent complex structures from copying correctly. Setting it
Expand Down Expand Up @@ -51,41 +49,6 @@ func DeepCopy(source interface{}, depth int) interface{} {

return r

case *data.Struct:
return v.Copy()

case *data.Array:
r := data.NewArray(v.Type(), v.Len())

for i := 0; i < v.Len(); i++ {
vv, _ := v.Get(i)
vv = DeepCopy(vv, depth-1)
_ = v.Set(i, vv)
}

return r

case *data.Map:
r := data.NewMap(v.KeyType(), v.ElementType())

for _, k := range v.Keys() {
d, _, _ := v.Get(k)
_, _ = r.Set(k, DeepCopy(d, depth-1))
}

return r

case *data.Package:
r := data.Package{}
keys := v.Keys()

for _, k := range keys {
d, _ := v.Get(k)
r.Set(k, DeepCopy(d, depth-1))
}

return &r

default:
return v
}
Expand Down
12 changes: 0 additions & 12 deletions expressions/builtins/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package builtins

import (
"github.com/tucats/gopackages/errors"
"github.com/tucats/gopackages/expressions/data"
"github.com/tucats/gopackages/expressions/symbols"
)

Expand All @@ -29,17 +28,6 @@ func Delete(s *symbols.SymbolTable, args []interface{}) (interface{}, error) {

return nil, s.Delete(v, false)

case *data.Map:
_, err := v.Delete(args[1])

return v, err

case *data.Array:
i := data.Int(args[1])
err := v.Delete(i)

return v, err

default:
return nil, errors.ErrInvalidType.In("delete")
}
Expand Down
Loading

0 comments on commit 0b27e11

Please sign in to comment.