Skip to content

Commit

Permalink
First commit of the new settings management library.
Browse files Browse the repository at this point in the history
The database model is updated but it maintains compatibility with the old database schema.
  • Loading branch information
Davide bonomini committed Jan 25, 2024
1 parent 85d14cf commit 37695b9
Show file tree
Hide file tree
Showing 12 changed files with 406 additions and 125 deletions.
11 changes: 6 additions & 5 deletions evo.database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package evo

import (
"fmt"
"log"
"os"
"strings"
"time"

"github.com/getevo/evo/v2/lib/db/schema"
"github.com/getevo/evo/v2/lib/settings"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"strings"
"time"
)

var db *gorm.DB
Expand All @@ -26,7 +27,7 @@ func setupDatabase() {
if !config.Enabled {
return
}
var logLevel = logger.Silent
var logLevel logger.LogLevel

switch config.Debug {
case 4:
Expand Down
6 changes: 5 additions & 1 deletion evo.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package evo

import (
"log"

dbo "github.com/getevo/evo/v2/lib/db"
"github.com/getevo/evo/v2/lib/generic"
"github.com/getevo/evo/v2/lib/memo"
"github.com/getevo/evo/v2/lib/settings"
"github.com/getevo/evo/v2/lib/settings/database"
"github.com/gofiber/fiber/v2"
"log"
)

var (
Expand All @@ -27,6 +28,9 @@ func Setup() {
settings.Register("HTTP", &http)
settings.Get("HTTP").Cast(&http)
err = generic.Parse(http).Cast(&fiberConfig)
if err != nil {
log.Fatal("Unable to retrieve HTTP server configurations: ", err)
}

app = fiber.New(fiberConfig)
if settings.Get("Database.Enabled").Bool() {
Expand Down
3 changes: 2 additions & 1 deletion lib/async/error_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package async

import (
"context"
"github.com/getevo/evo/v2/lib/async/multierror"
"sync"

"github.com/getevo/evo/v2/lib/async/multierror"

Check failure on line 7 in lib/async/error_pool.go

View workflow job for this annotation

GitHub Actions / build (1.19.x)

no required module provides package github.com/getevo/evo/v2/lib/async/multierror; to add it:

Check failure on line 7 in lib/async/error_pool.go

View workflow job for this annotation

GitHub Actions / build (1.20.x)

no required module provides package github.com/getevo/evo/v2/lib/async/multierror; to add it:
)

// ErrorPool is a pool that runs tasks that may return an error.
Expand Down
3 changes: 2 additions & 1 deletion lib/connectors/nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package nats

import (
"fmt"
"time"

"github.com/getevo/evo/v2/lib/log"
"github.com/getevo/evo/v2/lib/memo/kv"
"github.com/getevo/evo/v2/lib/pubsub"
"github.com/getevo/evo/v2/lib/serializer"
"github.com/getevo/evo/v2/lib/settings"
"github.com/nats-io/nats.go"
"time"
)

var Driver = NATS{}
Expand Down
34 changes: 25 additions & 9 deletions lib/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/araddon/dateparse"
"github.com/getevo/evo/v2/lib/log"
"github.com/iancoleman/strcase"
"gopkg.in/yaml.v3"
"reflect"
"regexp"
"strconv"
"strings"
"time"

"github.com/araddon/dateparse"
"github.com/getevo/evo/v2/lib/log"
"github.com/iancoleman/strcase"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -463,17 +464,17 @@ func (v Value) Cast(dst any) error {
for _, prop := range v.Props() {

if vRef.Kind() == reflect.Struct {
if x.HasProp(strcase.ToCamel(prop.Name)) {
x.SetProp(prop.Name, vRef.FieldByName(prop.Name).Interface())
if x.HasProp(prop.Name) {
x.SetProp(x.GetName(prop.Name), vRef.FieldByName(prop.Name).Interface())
}
} else if vRef.Kind() == reflect.Map {

if idx := vRef.MapIndex(reflect.ValueOf(prop.Name)); idx.IsValid() {
x.SetProp(strcase.ToCamel(prop.Name), idx.Interface())
x.SetProp(x.GetName(prop.Name), idx.Interface())
continue
}
if idx := vRef.MapIndex(reflect.ValueOf(strcase.ToSnake(prop.Name))); idx.IsValid() {
x.SetProp(strcase.ToCamel(prop.Name), idx.Interface())
x.SetProp(x.GetName(prop.Name), idx.Interface())
}

} else {
Expand All @@ -492,6 +493,7 @@ func (v Value) Cast(dst any) error {
x.SetProp(prop.Name, v.Prop(prop.Name).Input)
}
}

var x any
switch kind {
case reflect.Int:
Expand Down Expand Up @@ -575,7 +577,7 @@ func (v Value) Cast(dst any) error {
case reflect.Func, reflect.Struct, reflect.Interface:
return nil
default:
return fmt.Errorf("couldnt convert to %s", ref.String())
return fmt.Errorf("couldnt convert to %s %s", ref.String(), v.String())
}
ref.Set(reflect.ValueOf(x).Convert(ref.Type()))
return nil
Expand Down Expand Up @@ -616,6 +618,20 @@ func (v Value) HasProp(name string) bool {
return false
}

func (v Value) GetName(name string) string {
var typ = v.IndirectType()

toMatch := strings.ReplaceAll(strings.ToLower(name), "_", "")

for i := 0; i < typ.NumField(); i++ {
var field = typ.Field(i)
if strings.ReplaceAll(strings.ToLower(field.Name), "_", "") == toMatch {
return field.Name
}
}
return ""
}

func (v Value) Indirect() reflect.Value {
return indirect(v.Input)
}
Expand Down
7 changes: 4 additions & 3 deletions lib/memo/drivers/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package memory

import (
"fmt"
"sync"
"time"

"github.com/getevo/evo/v2/lib/log"
"github.com/getevo/evo/v2/lib/memo/kv"
"github.com/getevo/evo/v2/lib/serializer"
"github.com/getevo/evo/v2/lib/settings"
"sync"
"time"
)

var Driver = driver{}
Expand All @@ -28,7 +29,7 @@ func (driver) Register() error {
settings.Register(
settings.SettingDomain{
Title: "Cache",
Domain: "Cache",
Domain: "CACHE",
Description: "system cache configurations",
ReadOnly: false,
Visible: true,
Expand Down
Loading

0 comments on commit 37695b9

Please sign in to comment.