Skip to content

Commit

Permalink
Fix property parsing.
Browse files Browse the repository at this point in the history
* Fix URLProperty and FileProperty parsing.
* Added SelectOptionProperty to distinguish page select property and
database select property. Page uses SelectOptionProperty and database
uses SelectProperty.
  • Loading branch information
shayanh authored and jomei committed Jul 19, 2021
1 parent 60e0f56 commit cd9dc6d
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package notionapi
import (
"encoding/json"
"fmt"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -90,6 +91,20 @@ type SelectProperty struct {
Select Select `json:"select"`
}

func (p SelectProperty) GetType() PropertyType {
return p.Type
}

type SelectOptionProperty struct {
ID ObjectID `json:"id,omitempty"`
Type PropertyType `json:"type"`
Select Option `json:"select"`
}

func (p SelectOptionProperty) GetType() PropertyType {
return p.Type
}

type Select struct {
Options []Option `json:"options"`
}
Expand Down Expand Up @@ -120,10 +135,6 @@ type Option struct {
Color Color `json:"color"`
}

func (p SelectProperty) GetType() PropertyType {
return p.Type
}

type DateProperty struct {
ID ObjectID `json:"id,omitempty"`
Type PropertyType `json:"type"`
Expand Down Expand Up @@ -315,7 +326,16 @@ func parseProperties(raw map[string]interface{}) (map[string]Property, error) {
p = &RichTextProperty{}
}
case PropertyTypeSelect:
p = &SelectProperty{}
selectMap, ok := v.(map[string]interface{})["select"].(map[string]interface{})
if !ok {
return nil, errors.Errorf("an error occured while parsing property type: %s", rawProperty)
}
_, found := selectMap["options"]
if found {
p = &SelectProperty{}
} else {
p = &SelectOptionProperty{}
}
case PropertyTypeMultiSelect:
switch v.(map[string]interface{})["multi_select"].(type) {
case map[string]interface{}:
Expand All @@ -330,6 +350,8 @@ func parseProperties(raw map[string]interface{}) (map[string]Property, error) {
case PropertyTypeEmail:
p = &EmailProperty{}
case PropertyTypeURL:
p = &URLProperty{}
case PropertyTypeFile:
p = &FileProperty{}
case PropertyTypePhoneNumber:
p = PhoneNumberProperty{}
Expand Down

0 comments on commit cd9dc6d

Please sign in to comment.