-
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.
Rearrange pkgs to be usable as an importable library (#5)
- Loading branch information
Showing
13 changed files
with
172 additions
and
106 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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,43 @@ | ||
package crib | ||
|
||
import ( | ||
"fmt" | ||
"github.com/peppys/crib/pkg/crib/estimators" | ||
) | ||
|
||
type Crib struct { | ||
estimate estimators.Estimator | ||
} | ||
|
||
type Option func(c *Crib) | ||
|
||
func NewCrib(opts ...Option) *Crib { | ||
c := &Crib{} | ||
|
||
for _, opt := range opts { | ||
opt(c) | ||
} | ||
|
||
return c | ||
} | ||
|
||
func WithEstimator(e estimators.Estimator) Option { | ||
return func(c *Crib) { | ||
c.estimate = e | ||
} | ||
} | ||
|
||
func WithEstimators(e ...estimators.Estimator) Option { | ||
return func(c *Crib) { | ||
c.estimate = estimators.NewBulkEstimator(e...) | ||
} | ||
} | ||
|
||
func (c *Crib) Estimate(address string) ([]estimators.Estimate, error) { | ||
estimate, err := c.estimate(address) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while estimating valuation: %w", err) | ||
} | ||
|
||
return estimate, 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,40 @@ | ||
package estimators | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"sync" | ||
) | ||
|
||
func NewBulkEstimator(estimators ...Estimator) func(address string) ([]Estimate, error) { | ||
return func(address string) ([]Estimate, error) { | ||
var wg sync.WaitGroup | ||
var estimatorErrors []error | ||
var estimates []Estimate | ||
|
||
for _, estimator := range estimators { | ||
wg.Add(1) | ||
|
||
go func(estimator Estimator) { | ||
defer wg.Done() | ||
result, err := estimator(address) | ||
if err != nil { | ||
estimatorErrors = append(estimatorErrors, err) | ||
} else { | ||
estimates = append(estimates, result...) | ||
} | ||
}(estimator) | ||
} | ||
|
||
wg.Wait() | ||
if len(estimatorErrors) > 0 { | ||
err := errors.New("") | ||
for _, e := range estimatorErrors { | ||
err = errors.Wrapf(err, "estimator failed: %s", e.Error()) | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
return estimates, 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,15 @@ | ||
package estimators | ||
|
||
type Vendor string | ||
|
||
const ( | ||
Zillow Vendor = "ZILLOW" | ||
Redfin = "REDFIN" | ||
) | ||
|
||
type Estimate struct { | ||
Vendor Vendor | ||
Value float64 | ||
} | ||
|
||
type Estimator func(string) ([]Estimate, error) |
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