Skip to content

Commit

Permalink
add NullSnowflake converters
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelwing committed Dec 5, 2022
1 parent 14d687a commit e31b0d1
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions null_snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ func NewNullSnowflake(s Snowflake, valid bool) NullSnowflake {
}
}

// NullSnowflakeFromPtr creates a new NullSnowflake from a Snowflake pointer
func NullSnowflakeFromPtr(s *Snowflake) NullSnowflake {
if s == nil {
return NewNullSnowflake(Snowflake(0), false)
}

return NewNullSnowflake(*s, true)
}

// NullSnowflakeFromStringPtr creates a new NullSnowflake from a string pointer
// Always returns a NullSnowflake, will be invalid if the string cannot be converted to an int
func NullSnowflakeFromStringPtr(s *string) NullSnowflake {
if s == nil {
return NewNullSnowflake(Snowflake(0), false)
}

snowflake, err := SnowflakeFromString(*s)
if err != nil {
return NewNullSnowflake(Snowflake(0), false)
}

return NewNullSnowflake(snowflake, true)
}

// Scan implements sql.Scanner interface
func (s *NullSnowflake) Scan(value any) error {
if value == nil {
Expand Down

0 comments on commit e31b0d1

Please sign in to comment.