-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prefixed human readable id option, change mapping id default (
#78) * feat: add prefixed human readable id option, change mapping id to not be included by default Signed-off-by: Sarah Funkhouser <[email protected]> * add tests Signed-off-by: Sarah Funkhouser <[email protected]> --------- Signed-off-by: Sarah Funkhouser <[email protected]>
- Loading branch information
1 parent
41e917d
commit a6ae194
Showing
3 changed files
with
193 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package customtypes | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"entgo.io/ent/schema/field" | ||
) | ||
|
||
// PrefixedIdentifier is a custom type that implements the TypeValueScanner interface | ||
type PrefixedIdentifier struct { | ||
prefix string | ||
} | ||
|
||
// NewPrefixedIdentifier returns a new PrefixedIdentifier with the given prefix | ||
func NewPrefixedIdentifier(prefix string) PrefixedIdentifier { | ||
return PrefixedIdentifier{prefix: prefix} | ||
} | ||
|
||
// Value implements the TypeValueScanner.Value method. | ||
func (p PrefixedIdentifier) Value(s string) (driver.Value, error) { | ||
value := strings.TrimPrefix(s, p.prefix+"-") | ||
if value == "" { | ||
return "", nil | ||
} | ||
|
||
trimValue, err := strconv.Atoi(value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return fmt.Sprintf("%d", trimValue), nil | ||
} | ||
|
||
// ScanValue implements the TypeValueScanner.ScanValue method. | ||
func (PrefixedIdentifier) ScanValue() field.ValueScanner { | ||
return &sql.NullString{} | ||
} | ||
|
||
// FromValue implements the TypeValueScanner.FromValue method. | ||
func (p PrefixedIdentifier) FromValue(v driver.Value) (string, error) { | ||
s, ok := v.(*sql.NullString) | ||
if !ok { | ||
return "", fmt.Errorf("unexpected input for FromValue: %T", v) // nolint:err113 | ||
} | ||
|
||
if !s.Valid { | ||
return "", nil | ||
} | ||
|
||
return fmt.Sprintf("%s-%06s", p.prefix, s.String), nil | ||
} |
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,96 @@ | ||
package customtypes | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPrefixedIdentifierValue(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
prefix string | ||
input string | ||
want driver.Value | ||
}{ | ||
{ | ||
name: "with prefix", | ||
prefix: "test", | ||
input: "test-000001", | ||
want: "1", | ||
}, | ||
{ | ||
name: "without prefix", | ||
prefix: "test", | ||
input: "123", | ||
want: "123", | ||
}, | ||
{ | ||
name: "empty input", | ||
prefix: "test", | ||
input: "", | ||
want: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
p := PrefixedIdentifier{prefix: tt.prefix} | ||
got, err := p.Value(tt.input) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
func TestPrefixedIdentifierFromValue(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
prefix string | ||
input driver.Value | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid input", | ||
prefix: "test", | ||
input: &sql.NullString{String: "1", Valid: true}, | ||
want: "test-000001", | ||
}, | ||
{ | ||
name: "valid input", | ||
prefix: "test", | ||
input: &sql.NullString{String: "999999", Valid: true}, | ||
want: "test-999999", | ||
}, | ||
{ | ||
name: "invalid input type", | ||
prefix: "test", | ||
input: "invalid", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "null string input", | ||
prefix: "test", | ||
input: &sql.NullString{String: "", Valid: false}, | ||
want: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
p := PrefixedIdentifier{prefix: tt.prefix} | ||
|
||
got, err := p.FromValue(tt.input) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
} | ||
}) | ||
} | ||
} |
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