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

v0.0.3 #2

Merged
merged 3 commits into from
May 4, 2022
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
7 changes: 4 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Build and test
on:
push:
branches:
- main
- '*'
pull_request:
branches:
- main
Expand All @@ -25,8 +25,6 @@ jobs:

- name: Run linters
uses: golangci/golangci-lint-action@v2
with:
args: -D unused -D staticcheck -D gosimple

build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -61,6 +59,7 @@ jobs:

#Trying https://docs.github.com/en/actions/using-containerized-services/creating-postgresql-service-containers
test-postgresql:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest

services:
Expand Down Expand Up @@ -104,6 +103,7 @@ jobs:
GCA_CONFIG_FILE: ${{ github.workspace }}/test/yaml/gcaconfig_pgsql.yaml

test-mysql:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest

services:
Expand Down Expand Up @@ -148,6 +148,7 @@ jobs:
GCA_API_PORT: ${{ job.services.mysqldb.ports['3306'] }}

test-sqlserver:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest

services:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Those go packages are used :
- [ ] Add a github workflow
- [X] Init
- [X] Add pgsql, mysql and sqlserver testing
- [ ] Find why somes linters are not working : linked to https://github.com/golang/go/issues/26863, should rewrite condition interface. See also https://github.com/golang/go/issues/28254#issuecomment-996822778
- [X] Find why somes linters are not working : linked to https://github.com/golang/go/issues/26863, should rewrite condition interface. See also https://github.com/golang/go/issues/28254#issuecomment-996822778
- [X] Release pipeline
- [ ] Add an alter table function for sqlite (create new table, copy data, drop old table)
- [X] Review packages structure
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/geojsoncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func (gc *GeoJsonController) list(w http.ResponseWriter, r *http.Request) {
} else {
gc.responder.Success(result, w)
}
return
}

func (gc *GeoJsonController) read(w http.ResponseWriter, r *http.Request) {
Expand All @@ -49,7 +48,7 @@ func (gc *GeoJsonController) read(w http.ResponseWriter, r *http.Request) {
}
params := utils.GetRequestParams(r)
id := mux.Vars(r)["id"]
if strings.Index(id, ",") != -1 {
if strings.Contains(id, ",") {
ids := strings.Split(id, `,`)
results := struct {
Type string `json:"type"`
Expand Down
5 changes: 2 additions & 3 deletions pkg/controller/recordcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func (rc *RecordController) list(w http.ResponseWriter, r *http.Request) {
}
result := rc.service.List(table, params)
rc.responder.Success(result, w)
return
}

type argumentList struct {
Expand All @@ -56,7 +55,7 @@ func (rc *RecordController) read(w http.ResponseWriter, r *http.Request) {
}
params := utils.GetRequestParams(r)
id := mux.Vars(r)["id"]
if strings.Index(id, ",") != -1 {
if strings.Contains(id, ",") {
ids := strings.Split(id, `,`)
var argumentLists []*argumentList
for i := 0; i < len(ids); i++ {
Expand Down Expand Up @@ -195,7 +194,7 @@ func (rc *RecordController) delete(w http.ResponseWriter, r *http.Request) {
}
params := utils.GetRequestParams(r)
id := mux.Vars(r)["id"]
if strings.Index(id, ",") != -1 {
if strings.Contains(id, ",") {
ids := strings.Split(id, `,`)
var argumentLists []*argumentList
for i := 0; i < len(ids); i++ {
Expand Down
122 changes: 68 additions & 54 deletions pkg/database/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,41 @@ import (

// Condition interface
type Condition interface {
GetCondition() interface{ Condition }
And(condition interface{ Condition }) interface{ Condition }
Or(condition interface{ Condition }) interface{ Condition }
Not() interface{ Condition }
GetCondition() interface{}
And(condition interface{}) interface{}
Or(condition interface{}) interface{}
Not() interface{}
}

type GenericCondition struct {
condition interface{ Condition }
}

func (gc *GenericCondition) And(condition interface{ Condition }) interface{ Condition } {
switch condition.(type) {
func (gc *GenericCondition) And(condition interface{}) interface{} {
switch c := condition.(type) {
case *NoCondition:
return gc.condition
default:
return NewAndCondition(gc.condition, condition)
case interface{ Condition }:
return NewAndCondition(gc.condition, c)
}
return nil
}

func (gc *GenericCondition) Or(condition interface{ Condition }) interface{ Condition } {
switch condition.(type) {
func (gc *GenericCondition) Or(condition interface{}) interface{} {
switch c := condition.(type) {
case *NoCondition:
return gc.condition
default:
return NewOrCondition(gc.condition, condition)
case interface{ Condition }:
return NewOrCondition(gc.condition, c)
}
return nil
}

func (gc *GenericCondition) Not() interface{ Condition } {
func (gc *GenericCondition) Not() interface{} {
return NewNotCondition(gc.condition)
}

func (gc *GenericCondition) GetCondition() interface{ Condition } {
func (gc *GenericCondition) GetCondition() interface{} {
return nil
}

Expand Down Expand Up @@ -76,7 +78,7 @@ func ConditionFromString(table *ReflectedTable, value string) interface{ Conditi
condition = NewColumnCondition(field, command, parts[2])
}
if negate {
condition = condition.Not()
condition = condition.Not().(interface{ Condition })
}
return condition
}
Expand All @@ -92,19 +94,19 @@ func NewNoCondition() *NoCondition {

}

func (nc *NoCondition) GetCondition() interface{ Condition } {
func (nc *NoCondition) GetCondition() interface{} {
return nil
}

func (nc *NoCondition) And(condition interface{ Condition }) interface{ Condition } {
func (nc *NoCondition) And(condition interface{}) interface{} {
return condition
}

func (nc *NoCondition) Or(condition interface{ Condition }) interface{ Condition } {
func (nc *NoCondition) Or(condition interface{}) interface{} {
return condition
}

func (nc *NoCondition) Not() interface{ Condition } {
func (nc *NoCondition) Not() interface{} {
return nc
}

Expand All @@ -120,25 +122,27 @@ func NewNotCondition(condition interface{ Condition }) *NotCondition {
return &NotCondition{condition, GenericCondition{condition}}
}

func (nc *NotCondition) And(condition interface{ Condition }) interface{ Condition } {
switch condition.(type) {
func (nc *NotCondition) And(condition interface{}) interface{} {
switch c := condition.(type) {
case *NoCondition:
return nc
default:
return NewAndCondition(nc, condition)
case interface{ Condition }:
return NewAndCondition(nc, c)
}
return nil
}

func (nc *NotCondition) Or(condition interface{ Condition }) interface{ Condition } {
switch condition.(type) {
func (nc *NotCondition) Or(condition interface{}) interface{} {
switch c := condition.(type) {
case *NoCondition:
return nc
default:
return NewOrCondition(nc, condition)
case interface{ Condition }:
return NewOrCondition(nc, c)
}
return nil
}

func (nc *NotCondition) GetCondition() interface{ Condition } {
func (nc *NotCondition) GetCondition() interface{} {
return nc.condition
}

Expand All @@ -154,23 +158,25 @@ func NewOrCondition(condition1, condition2 interface{ Condition }) *OrCondition
return &OrCondition{[]interface{ Condition }{condition1, condition2}, GenericCondition{}}
}

func (oc *OrCondition) Or(condition interface{ Condition }) interface{ Condition } {
switch condition.(type) {
func (oc *OrCondition) Or(condition interface{}) interface{} {
switch c := condition.(type) {
case *NoCondition:
return oc
default:
oc.conditions = append(oc.conditions, condition)
case interface{ Condition }:
oc.conditions = append(oc.conditions, c)
return oc
}
return nil
}

func (oc *OrCondition) And(condition interface{ Condition }) interface{ Condition } {
switch condition.(type) {
func (oc *OrCondition) And(condition interface{}) interface{} {
switch c := condition.(type) {
case *NoCondition:
return oc
default:
return NewAndCondition(oc, condition)
case interface{ Condition }:
return NewAndCondition(oc, c)
}
return nil
}

func (ac *OrCondition) GetConditions() []interface{ Condition } {
Expand All @@ -181,7 +187,9 @@ func OrConditionFromArray(conditions []interface{ Condition }) interface{ Condit
var condition interface{ Condition }
condition = NewNoCondition()
for _, c := range conditions {
condition = condition.Or(c)
if ct, ok := condition.Or(c).(interface{ Condition }); ok {
condition = ct
}
}
return condition
}
Expand All @@ -198,23 +206,25 @@ func NewAndCondition(condition1, condition2 interface{ Condition }) *AndConditio
return &AndCondition{[]interface{ Condition }{condition1, condition2}, GenericCondition{}}
}

func (ac *AndCondition) And(condition interface{ Condition }) interface{ Condition } {
switch condition.(type) {
func (ac *AndCondition) And(condition interface{}) interface{} {
switch c := condition.(type) {
case *NoCondition:
return ac
default:
ac.conditions = append(ac.conditions, condition)
case interface{ Condition }:
ac.conditions = append(ac.conditions, c)
return ac
}
return nil
}

func (ac *AndCondition) Or(condition interface{ Condition }) interface{ Condition } {
switch condition.(type) {
func (ac *AndCondition) Or(condition interface{}) interface{} {
switch c := condition.(type) {
case *NoCondition:
return ac
default:
return NewOrCondition(ac, condition)
case interface{ Condition }:
return NewOrCondition(ac, c)
}
return nil
}

func (ac *AndCondition) GetConditions() []interface{ Condition } {
Expand All @@ -225,7 +235,9 @@ func AndConditionFromArray(conditions []interface{ Condition }) interface{ Condi
var condition interface{ Condition }
condition = NewNoCondition()
for _, c := range conditions {
condition = condition.And(c)
if ct, ok := condition.And(c).(interface{ Condition }); ok {
condition = ct
}
}
return condition
}
Expand Down Expand Up @@ -273,20 +285,22 @@ func NewSpatialCondition(column *ReflectedColumn, operator, value string) *Spati
return &SpatialCondition{*condition}
}

func (sc *SpatialCondition) And(condition interface{ Condition }) interface{ Condition } {
switch condition.(type) {
func (sc *SpatialCondition) And(condition interface{}) interface{} {
switch c := condition.(type) {
case *NoCondition:
return sc
default:
return NewAndCondition(sc, condition)
case interface{ Condition }:
return NewAndCondition(sc, c)
}
return nil
}

func (sc *SpatialCondition) Or(condition interface{ Condition }) interface{ Condition } {
switch condition.(type) {
func (sc *SpatialCondition) Or(condition interface{}) interface{} {
switch c := condition.(type) {
case *NoCondition:
return sc
default:
return NewOrCondition(sc, condition)
case interface{ Condition }:
return NewOrCondition(sc, c)
}
return nil
}
5 changes: 2 additions & 3 deletions pkg/database/conditionsbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (cb *ConditionsBuilder) getOrConditionSql(or *OrCondition, arguments *[]int
}

func (cb *ConditionsBuilder) getNotConditionSql(not *NotCondition, arguments *[]interface{}) string {
condition := not.GetCondition()
condition := not.GetCondition().(interface{ Condition })
return "(NOT " + cb.getConditionSql(condition, arguments) + ")"
}

Expand All @@ -70,9 +70,8 @@ func (cb *ConditionsBuilder) escapeLikeValue(value string) string {
// Addcslashes - Quote string with slashes in a C style
func (cb *ConditionsBuilder) addcslashes(s string, c string) string {
var tmpRune []rune
strRune := []rune(s)
list := []rune(c)
for _, ch := range strRune {
for _, ch := range s {
for _, v := range list {
if ch == v {
tmpRune = append(tmpRune, '\\')
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/genericdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ func (g *GenericDB) RollBackTransaction(tx *sql.Tx) error {
func (g *GenericDB) addMiddlewareConditions(tableName string, condition interface{ Condition }) interface{ Condition } {
condition1 := g.VariableStore.Get("authorization.conditions." + tableName)
if condition1 != nil {
condition = condition.And(condition1.(interface{ Condition }))
condition = condition.And(condition1).(interface{ Condition })
}
condition2 := g.VariableStore.Get("multiTenancy.conditions." + tableName)
if condition2 != nil {
condition = condition.And(condition2.(interface{ Condition }))
condition = condition.And(condition2).(interface{ Condition })
}
return condition
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/realnamemapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewRealNameMapper(mapping map[string]string) *RealNameMapper {
columnMapping := map[string]map[string]string{}
reverseColumnMapping := map[string]map[string]string{}
for name, realName := range mapping {
if strings.Index(name, ".") >= 0 && strings.Index(realName, ".") >= 0 {
if strings.Contains(name, ".") && strings.Contains(realName, ".") {
nameSplit := strings.SplitN(name, ".", 2)
realNameSplit := strings.SplitN(realName, ".", 2)
tableMapping[nameSplit[0]] = realNameSplit[0]
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/reflectedtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func NewReflectedTableFromReflection(reflection *GenericReflection, name, realNa
tableName := table["TABLE_NAME"].(string)
suffix := tableName + "_id"
if columnName[len(columnName)-len(suffix):] == suffix {
column.SetFk(tableName)
}
column.SetFk(tableName)
}
}
}
Expand Down
Loading