Skip to content

Commit

Permalink
fix(rpc) apply param scan
Browse files Browse the repository at this point in the history
  • Loading branch information
vani-rf committed Mar 2, 2025
1 parent 1213f74 commit f6e3ada
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 50 deletions.
4 changes: 0 additions & 4 deletions pkg/generator/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ func (r *{{.Name | ToGoIdentifier }}) Enums() []string {
return {{ .Enums }}
}
func (r *{{.Name | ToGoIdentifier }}) Attributes() []string {
return {{ .Attributes }}
}
func (r *{{.Name | ToGoIdentifier }}) Comment() *string {
return {{ .Comment }}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/generator/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestGenerateTypes(t *testing.T) {
Schema: "public",
Format: "",
Enums: []string{"test_1", "test_2"},
Attributes: []string{},
Attributes: []objects.TypeAttribute{},
Comment: nil,
},
}
Expand Down
42 changes: 23 additions & 19 deletions pkg/resource/types/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@ import (
func TestCompare(t *testing.T) {
source := []objects.Type{
{
Name: "type1_updated",
Schema: "public",
Format: "",
Enums: []string{"test_1", "test_2"},
Attributes: []string{"attribute_1"},
Comment: nil,
Name: "type1_updated",
Schema: "public",
Format: "",
Enums: []string{"test_1", "test_2"},
Attributes: []objects.TypeAttribute{{
Name: "type_attribute_1",
}},
Comment: nil,
},
}

target := []objects.Type{
{
Name: "type1_updated",
Schema: "public",
Format: "",
Enums: []string{"test_1", "test_2"},
Attributes: []string{"attribute_1"},
Comment: nil,
Name: "type1_updated",
Schema: "public",
Format: "",
Enums: []string{"test_1", "test_2"},
Attributes: []objects.TypeAttribute{{
Name: "type_attribute_1",
}},
Comment: nil,
},
}

Expand All @@ -43,7 +47,7 @@ func TestCompareList(t *testing.T) {
Schema: "public",
Format: "",
Enums: []string{"test_1", "test_2"},
Attributes: []string{},
Attributes: []objects.TypeAttribute{},
Comment: nil,
},
{
Expand All @@ -55,14 +59,14 @@ func TestCompareList(t *testing.T) {
Name: "type3",
Schema: "auth",
Comment: &sourceComment,
Attributes: []string{"attribute_1"},
Attributes: []objects.TypeAttribute{{Name: "type_attribute_1"}},
},
{
Name: "type4",
Schema: "auth",
Comment: &sourceComment,
Enums: []string{"enum_x"},
Attributes: []string{"attribute_x"},
Attributes: []objects.TypeAttribute{{Name: "type_attribute_x"}},
},
}

Expand All @@ -73,27 +77,27 @@ func TestCompareList(t *testing.T) {
Schema: "public",
Format: "",
Enums: []string{"test_1", "test_2", "test_3"},
Attributes: []string{"attribute_1"},
Attributes: []objects.TypeAttribute{{Name: "type_attribute_1"}},
Comment: nil,
},
{
Name: "type2",
Schema: "public",
Comment: &targetComment,
Attributes: []string{"attribute_1"},
Attributes: []objects.TypeAttribute{{Name: "type_attribute_1"}},
},
{
Name: "type3",
Schema: "auth",
Comment: &targetComment,
Attributes: []string{"attribute_1", "attribute_2"},
Attributes: []objects.TypeAttribute{{Name: "type_attribute_1"}, {Name: "type_attribute_2"}},
},
{
Name: "type4",
Schema: "auth",
Comment: &sourceComment,
Enums: []string{"enum_x"},
Attributes: []string{"attribute_x"},
Attributes: []objects.TypeAttribute{{Name: "type_attribute_x"}},
},
}

Expand Down
4 changes: 0 additions & 4 deletions pkg/resource/types/print_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,6 @@ func GenerateDiffChangeUpdateMessage(name string, item MigrateItem) (string, err
oldData := strings.Join(item.OldData.Enums, ",")
newData := strings.Join(item.NewData.Enums, ",")
changeMsgArr = append(changeMsgArr, fmt.Sprintf("- %s : %v >>> %v", "enums", oldData, newData))
case objects.UpdateTypeAttributes:
oldData := strings.Join(item.OldData.Attributes, ",")
newData := strings.Join(item.NewData.Attributes, ",")
changeMsgArr = append(changeMsgArr, fmt.Sprintf("- %s : %v >>> %v", "attributes", oldData, newData))
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/resource/types/print_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ func TestPrintDiff(t *testing.T) {
Schema: "public",
Format: "",
Enums: []string{"test_1", "test_2"},
Attributes: []string{},
Attributes: []objects.TypeAttribute{},
Comment: nil,
},
TargetResource: objects.Type{
Name: "target_type",
Schema: "test",
Format: "",
Enums: []string{"test_1", "test_3"},
Attributes: []string{},
Attributes: []objects.TypeAttribute{},
Comment: nil,
},
DiffItems: migratedItems,
Expand Down
3 changes: 2 additions & 1 deletion pkg/state/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func BindRpcFunction(rpc raiden.Rpc, fn *objects.Function) (err error) {
fn.CompleteStatement = rpc.GetCompleteStmt()

// validate definition query
matches := regexp.MustCompile(`:\w+`).FindAllString(fn.CompleteStatement, -1)
cleanStatement := strings.ReplaceAll(fn.CompleteStatement, "::", "")
matches := regexp.MustCompile(`:\w+`).FindAllString(cleanStatement, -1)
if len(matches) > 0 {
var errMsg string
if len(matches) > 1 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/state/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func (*MockType) Enums() []string {
return []string{"type_1", "type_2"}
}

func (*MockType) Attributes() []string {
return []string{}
func (*MockType) Attributes() []objects.TypeAttribute {
return []objects.TypeAttribute{}
}

func (*MockType) Comment() *string {
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestBuildTypeFromState(t *testing.T) {
Schema: "test",
Format: "type_1",
Enums: []string{"type_1", "type_2"},
Attributes: []string{},
Attributes: []objects.TypeAttribute{},
Comment: nil,
},
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/supabase/drivers/local/meta/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func GetTypes(cfg *raiden.Config, includedSchemas []string) ([]objects.Type, err
url := fmt.Sprintf("%s%s/types", cfg.SupabaseApiUrl, cfg.SupabaseApiBasePath)
reqInterceptor := func(req *http.Request) error {
if len(includedSchemas) > 0 {
req.URL.Query().Set("included_schemas", strings.Join(includedSchemas, ","))
reqQuery := req.URL.Query()
reqQuery.Set("included_schemas", strings.Join(includedSchemas, ","))
req.URL.RawQuery = reqQuery.Encode()
}

if cfg.SupabaseApiToken != "" {
Expand All @@ -27,6 +29,7 @@ func GetTypes(cfg *raiden.Config, includedSchemas []string) ([]objects.Type, err

return nil
}

rs, err := net.Get[[]objects.Type](url, net.DefaultTimeout, reqInterceptor, nil)
if err != nil {
err = fmt.Errorf("get types error : %s", err)
Expand Down
19 changes: 12 additions & 7 deletions pkg/supabase/objects/type.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package objects

type TypeAttribute struct {
Name string `json:"name"`
TypeID int `json:"type_id"`
}

type Type struct {
ID int `json:"id"`
Name string `json:"name"`
Schema string `json:"schema"`
Format string `json:"format"`
Enums []string `json:"enums"`
Attributes []string `json:"attributes"`
Comment *string `json:"comment"` // Use pointer to handle null values
ID int `json:"id"`
Name string `json:"name"`
Schema string `json:"schema"`
Format string `json:"format"`
Enums []string `json:"enums"`
Attributes []TypeAttribute `json:"attributes"`
Comment *string `json:"comment"` // Use pointer to handle null values
}

type UpdateDataType string
Expand Down
4 changes: 2 additions & 2 deletions pkg/supabase/supabase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,7 @@ func TestUpdateType_Cloud(t *testing.T) {
Schema: "public",
Format: "",
Enums: []string{"test_1"},
Attributes: []string{},
Attributes: []objects.TypeAttribute{},
Comment: nil,
}

Expand Down Expand Up @@ -1866,7 +1866,7 @@ func TestUpdateType_SelfHosted(t *testing.T) {
Schema: "public",
Format: "",
Enums: []string{"test_1"},
Attributes: []string{},
Attributes: []objects.TypeAttribute{},
Comment: nil,
}

Expand Down
18 changes: 16 additions & 2 deletions pkg/utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,28 @@ func MatchReplacer(query string, paramKey string, replacement string) string {

// Replace the parameter in the query, considering context
for i := 0; i < len(words); i++ {
if words[i] == paramKey {
wRaw := words[i]
if strings.Contains(wRaw, "::") {
wRaw = strings.SplitN(wRaw, "::", 2)[0]
}

if strings.Contains(wRaw, paramKey) {
re := regexp.MustCompile(`:\w+`)
// Find first match
wRaw = re.FindString(paramKey)
}

re := regexp.MustCompile(fmt.Sprintf(`[^a-zA-Z0-9%s\s]`, regexp.QuoteMeta(":")))
wCheck := re.ReplaceAllString(wRaw, "")
if wCheck == paramKey {

// Check if there is a dot (.) before or after the parameter
hasDotBefore := i > 0 && strings.HasSuffix(words[i-1], ".")
hasDotAfter := i < len(words)-1 && strings.HasPrefix(words[i+1], ".")

// Replace only if it's not part of a longer identifier (e.g., :u.Role)
if !hasDotBefore && !hasDotAfter {
words[i] = replacement
words[i] = strings.ReplaceAll(words[i], paramKey, replacement)
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package raiden

import "encoding/json"
import (
"encoding/json"

"github.com/sev-2/raiden/pkg/supabase/objects"
)

const (
DefaultTypeSchema = "public"
Expand All @@ -12,7 +16,7 @@ type (
Schema() string
Format() string
Enums() []string
Attributes() []string
Attributes() []objects.TypeAttribute
Comment() *string
}
)
Expand All @@ -38,8 +42,8 @@ func (*TypeBase) Enums() []string {
return []string{}
}

func (*TypeBase) Attributes() []string {
return []string{}
func (*TypeBase) Attributes() []objects.TypeAttribute {
return []objects.TypeAttribute{}
}

func (*TypeBase) Comment() *string {
Expand Down

0 comments on commit f6e3ada

Please sign in to comment.