Resolve usernames for person property type (#2116)

* resolve usernames for  property types
This commit is contained in:
Doug Lauder 2022-01-18 13:08:28 -05:00 committed by GitHub
parent 02dfb6eceb
commit 60af00ac3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View file

@ -19,6 +19,12 @@ var ErrInvalidPropertyValue = errors.New("invalid property value")
var ErrInvalidPropertyValueType = errors.New("invalid property value type") var ErrInvalidPropertyValueType = errors.New("invalid property value type")
var ErrInvalidDate = errors.New("invalid date property") var ErrInvalidDate = errors.New("invalid date property")
// PropValueResolver allows PropDef.GetValue to further decode property values, such as
// looking up usernames from ids.
type PropValueResolver interface {
GetUserByID(userID string) (*User, error)
}
// BlockProperties is a map of Prop's keyed by property id. // BlockProperties is a map of Prop's keyed by property id.
type BlockProperties map[string]BlockProp type BlockProperties map[string]BlockProp
@ -52,7 +58,7 @@ type PropDef struct {
// GetValue resolves the value of a property if the passed value is an ID for an option, // GetValue resolves the value of a property if the passed value is an ID for an option,
// otherwise returns the original value. // otherwise returns the original value.
func (pd PropDef) GetValue(v interface{}) (string, error) { func (pd PropDef) GetValue(v interface{}, resolver PropValueResolver) (string, error) {
switch pd.Type { switch pd.Type {
case "select": case "select":
// v is the id of an option // v is the id of an option
@ -76,12 +82,18 @@ func (pd PropDef) GetValue(v interface{}) (string, error) {
case "person": case "person":
// v is a userid // v is a userid
userid, ok := v.(string) userID, ok := v.(string)
if !ok { if !ok {
return "", ErrInvalidPropertyValueType return "", ErrInvalidPropertyValueType
} }
// TODO if resolver != nil {
return userid, nil user, err := resolver.GetUserByID(userID)
if err != nil {
return "", err
}
return user.Username, nil
}
return userID, nil
case "multiSelect": case "multiSelect":
// v is a slice of strings containing option ids // v is a slice of strings containing option ids
@ -202,8 +214,8 @@ func getMapString(key string, m map[string]interface{}) string {
} }
// ParseProperties parses a block's `Fields` to extract the properties. Properties typically exist on // ParseProperties parses a block's `Fields` to extract the properties. Properties typically exist on
// card blocks. // card blocks. A resolver can optionally be provided to fetch usernames for `person` prop type.
func ParseProperties(block *Block, schema PropSchema) (BlockProperties, error) { func ParseProperties(block *Block, schema PropSchema, resolver PropValueResolver) (BlockProperties, error) {
props := make(map[string]BlockProp) props := make(map[string]BlockProp)
if block == nil { if block == nil {
@ -236,7 +248,7 @@ func ParseProperties(block *Block, schema PropSchema) (BlockProperties, error) {
def, ok := schema[k] def, ok := schema[k]
if ok { if ok {
val, err := def.GetValue(v) val, err := def.GetValue(v, resolver)
if err != nil { if err != nil {
return props, fmt.Errorf("could not parse property value (%s): %w", fmt.Sprintf("%v", v), err) return props, fmt.Errorf("could not parse property value (%s): %w", fmt.Sprintf("%v", v), err)
} }

View file

@ -289,7 +289,7 @@ func (dg *diffGenerator) generateDiffForBlock(newBlock *model.Block, schema mode
func (dg *diffGenerator) generatePropDiffs(oldBlock, newBlock *model.Block, schema model.PropSchema) []PropDiff { func (dg *diffGenerator) generatePropDiffs(oldBlock, newBlock *model.Block, schema model.PropSchema) []PropDiff {
var propDiffs []PropDiff var propDiffs []PropDiff
oldProps, err := model.ParseProperties(oldBlock, schema) oldProps, err := model.ParseProperties(oldBlock, schema, dg.store)
if err != nil { if err != nil {
dg.logger.Error("Cannot parse properties for old block", dg.logger.Error("Cannot parse properties for old block",
mlog.String("block_id", oldBlock.ID), mlog.String("block_id", oldBlock.ID),
@ -297,7 +297,7 @@ func (dg *diffGenerator) generatePropDiffs(oldBlock, newBlock *model.Block, sche
) )
} }
newProps, err := model.ParseProperties(newBlock, schema) newProps, err := model.ParseProperties(newBlock, schema, dg.store)
if err != nil { if err != nil {
dg.logger.Error("Cannot parse properties for new block", dg.logger.Error("Cannot parse properties for new block",
mlog.String("block_id", oldBlock.ID), mlog.String("block_id", oldBlock.ID),