Skip to content

Commit

Permalink
Merge pull request #8 from reMarkable/feat/firestore-type
Browse files Browse the repository at this point in the history
feat: add firestore database type
  • Loading branch information
Zarux authored May 27, 2024
2 parents 715cd75 + 9b07cff commit 9ca6b47
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 8 deletions.
54 changes: 46 additions & 8 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ type Specification struct {
Property string `envconfig:"inner"`
PropertyWithDefault string `envconfig:"PROPERTYWITHDEFAULT" default:"fuzzybydefault"`
} `envconfig:"outer"`
AfterNested string `envconfig:"AFTERNESTED"`
DecodeStruct HonorDecodeInStruct `envconfig:"honor"`
Datetime time.Time `envconfig:"DATETIME"`
MapField map[string]string `envconfig:"MAPFIELD" default:"one:two;three:four"`
EmptyMapField map[string]string `envconfig:"EMPTY_MAPFIELD"`
UrlValue CustomURL `envconfig:"URLVALUE"`
UrlPointer *CustomURL `envconfig:"URLPOINTER"`
GooglePubSubTopic types.GooglePubSubTopic `envconfig:"GOOGLE_PUBSUB_TOPIC"`
AfterNested string `envconfig:"AFTERNESTED"`
DecodeStruct HonorDecodeInStruct `envconfig:"honor"`
Datetime time.Time `envconfig:"DATETIME"`
MapField map[string]string `envconfig:"MAPFIELD" default:"one:two;three:four"`
EmptyMapField map[string]string `envconfig:"EMPTY_MAPFIELD"`
UrlValue CustomURL `envconfig:"URLVALUE"`
UrlPointer *CustomURL `envconfig:"URLPOINTER"`
GooglePubSubTopic types.GooglePubSubTopic `envconfig:"GOOGLE_PUBSUB_TOPIC"`
GoogleFirestoreDatabase types.GoogleFirestoreDatabase `envconfig:"GOOGLE_FIRESTORE_DATABASE"`
}

type Embedded struct {
Expand Down Expand Up @@ -114,6 +115,7 @@ func TestProcess(t *testing.T) {
os.Setenv("ENV_CONFIG_URLVALUE", "https://github.com/kelseyhightower/envconfig")
os.Setenv("ENV_CONFIG_URLPOINTER", "https://github.com/kelseyhightower/envconfig")
os.Setenv("ENV_CONFIG_GOOGLE_PUBSUB_TOPIC", "projects/project-id/topics/topic-id")
os.Setenv("ENV_CONFIG_GOOGLE_FIRESTORE_DATABASE", "projects/project-id/databases/db")
err := Process("env_config", &s)
if err != nil {
t.Error(err.Error())
Expand Down Expand Up @@ -223,6 +225,14 @@ func TestProcess(t *testing.T) {
if s.GooglePubSubTopic.TopicID != "topic-id" {
t.Errorf("expected %s, got %s", "topic-id", s.GooglePubSubTopic.TopicID)
}

if s.GoogleFirestoreDatabase.ProjectID != "project-id" {
t.Errorf("expected %s, got %s", "project-id", s.GoogleFirestoreDatabase.ProjectID)
}

if s.GoogleFirestoreDatabase.Database != "db" {
t.Errorf("expected %s, got %s", "db", s.GoogleFirestoreDatabase.Database)
}
}

func TestParseErrorBool(t *testing.T) {
Expand Down Expand Up @@ -324,6 +334,34 @@ func TestParseErrorGooglePubSubTopic(t *testing.T) {
}
}

func TestParseErrorGoogleFirestoreDatabase(t *testing.T) {
var s Specification
os.Clearenv()
os.Setenv("ENV_CONFIG_GOOGLE_FIRESTORE_DATABASE", "invalid/project-id/databases")
os.Setenv("ENV_CONFIG_REQUIREDVAR", "foo")
err := Process("env_config", &s)
v, ok := err.(*ParseError)
if !ok {
t.Errorf("expected ParseError, got %v", v)
}

if v.FieldName != "GoogleFirestoreDatabase" {
t.Errorf("expected %s, got %v", "GoogleFirestoreDatabase", v.FieldName)
}

if s.GoogleFirestoreDatabase.Database != "" {
t.Errorf("expected %s, got %s", "", s.GoogleFirestoreDatabase.Database)
}

if s.GoogleFirestoreDatabase.ProjectID != "" {
t.Errorf("expected %s, got %s", "", s.GoogleFirestoreDatabase.ProjectID)
}

if v.Err != types.ErrInvalidGoogleFirestoreID {
t.Errorf("unexpected %s, got %s", types.ErrInvalidGoogleFirestoreID, v.Err)
}
}

func TestErrInvalidSpecification(t *testing.T) {
m := make(map[string]string)
err := Process("env_config", &m)
Expand Down
1 change: 1 addition & 0 deletions testdata/custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ ENV_CONFIG_EMPTY_MAPFIELD=
ENV_CONFIG_URLVALUE=
ENV_CONFIG_URLPOINTER=
ENV_CONFIG_GOOGLE_PUBSUB_TOPIC=
ENV_CONFIG_GOOGLE_FIRESTORE_DATABASE=
5 changes: 5 additions & 0 deletions testdata/default_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,8 @@ ENV_CONFIG_GOOGLE_PUBSUB_TOPIC
..[type]........GooglePubSubTopic
..[default].....
..[required]....
ENV_CONFIG_GOOGLE_FIRESTORE_DATABASE
..[description].
..[type]........GoogleFirestoreDatabase
..[default].....
..[required]....
1 change: 1 addition & 0 deletions testdata/default_table.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ ENV_CONFIG_EMPTY_MAPFIELD........................Semicolon-separated.list.of.Str
ENV_CONFIG_URLVALUE..............................CustomURL.............................................................................
ENV_CONFIG_URLPOINTER............................CustomURL.............................................................................
ENV_CONFIG_GOOGLE_PUBSUB_TOPIC...................GooglePubSubTopic.....................................................................
ENV_CONFIG_GOOGLE_FIRESTORE_DATABASE.............GoogleFirestoreDatabase...............................................................
1 change: 1 addition & 0 deletions testdata/fault.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@
{.Key}
{.Key}
{.Key}
{.Key}
32 changes: 32 additions & 0 deletions types/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"regexp"
)

// -----------------------------------------------------------------------------
// PUBSUB TOPIC
// -----------------------------------------------------------------------------

var (
// ErrInvalidGoogleTopicID means the configured topic has the wrong format.
ErrInvalidGoogleTopicID = errors.New("topic is not valid format")
Expand All @@ -28,3 +32,31 @@ func (pst *GooglePubSubTopic) Set(value string) error {

return nil
}

// -----------------------------------------------------------------------------
// FIRESTORE DATABASE
// -----------------------------------------------------------------------------

var (
// ErrInvalidGoogleFirestoreID means the configured database id has the wrong format.
ErrInvalidGoogleFirestoreID = errors.New("firestore id is not valid format")

googleFirestoreRegexp = regexp.MustCompile(`projects\/([\w-]+)\/databases\/([\w-]+)`)
)

type GoogleFirestoreDatabase struct {
ProjectID string
Database string
}

func (pst *GoogleFirestoreDatabase) Set(value string) error {
m := googleFirestoreRegexp.FindStringSubmatch(value)
if len(m) != 3 {
return ErrInvalidGoogleFirestoreID
}

pst.ProjectID = m[1]
pst.Database = m[2]

return nil
}

0 comments on commit 9ca6b47

Please sign in to comment.