-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for auto increment and not null in add rest handlers from…
… struct definition (#930)
- Loading branch information
1 parent
290d4e8
commit b98a7a3
Showing
6 changed files
with
395 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package gofr | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"gofr.dev/pkg/gofr/datasource/sql" | ||
) | ||
|
||
func getTableName(object any, structName string) string { | ||
if v, ok := object.(TableNameOverrider); ok { | ||
return v.TableName() | ||
} | ||
|
||
return toSnakeCase(structName) | ||
} | ||
|
||
func getRestPath(object any, structName string) string { | ||
if v, ok := object.(RestPathOverrider); ok { | ||
return v.RestPath() | ||
} | ||
|
||
return structName | ||
} | ||
|
||
func hasAutoIncrementID(constraints map[string]sql.FieldConstraints) bool { | ||
for _, constraint := range constraints { | ||
if constraint.AutoIncrement { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func parseSQLTag(inputTags reflect.StructTag) (sql.FieldConstraints, error) { | ||
var constraints sql.FieldConstraints | ||
|
||
sqlTag := inputTags.Get("sql") | ||
if sqlTag == "" { | ||
return constraints, nil | ||
} | ||
|
||
tags := strings.Split(sqlTag, ",") | ||
|
||
for _, tag := range tags { | ||
tag = strings.ToLower(tag) // Convert to lowercase for case-insensitivity | ||
|
||
switch tag { | ||
case "auto_increment": | ||
constraints.AutoIncrement = true | ||
case "not_null": | ||
constraints.NotNull = true | ||
default: | ||
return constraints, fmt.Errorf("%w: %s", errInvalidSQLTag, tag) | ||
} | ||
} | ||
|
||
return constraints, nil | ||
} | ||
|
||
func toSnakeCase(str string) string { | ||
diff := 'a' - 'A' | ||
length := len(str) | ||
|
||
var builder strings.Builder | ||
|
||
for i, char := range str { | ||
if char >= 'a' { | ||
builder.WriteRune(char) | ||
continue | ||
} | ||
|
||
if (i != 0 || i == length-1) && ((i > 0 && rune(str[i-1]) >= 'a') || (i < length-1 && rune(str[i+1]) >= 'a')) { | ||
builder.WriteRune('_') | ||
} | ||
|
||
builder.WriteRune(char + diff) | ||
} | ||
|
||
return builder.String() | ||
} |
Oops, something went wrong.