89 lines
2.9 KiB
Go
89 lines
2.9 KiB
Go
package notionmapper
|
|
|
|
import "github.com/jomei/notionapi"
|
|
|
|
// PropertyExtractor è una funzione che estrae un valore da una Property di Notion
|
|
type PropertyExtractor[T any] func(prop notionapi.Property) T
|
|
|
|
// NotionMapper mappa le properties di Notion a una struct Go
|
|
type NotionMapper[TDest any] struct {
|
|
propertyMappings map[string]func(notionapi.Property, *TDest)
|
|
pageMappings []func(notionapi.Page, *TDest)
|
|
}
|
|
|
|
// NewMapper crea un nuovo mapper per il tipo di destinazione specificato
|
|
func NewMapper[TDest any]() *NotionMapper[TDest] {
|
|
return &NotionMapper[TDest]{
|
|
propertyMappings: make(map[string]func(notionapi.Property, *TDest)),
|
|
pageMappings: make([]func(notionapi.Page, *TDest), 0),
|
|
}
|
|
}
|
|
|
|
// ForMember configura come mappare una property specifica
|
|
// propertyName: nome della property in Notion
|
|
// setter: funzione che setta il valore sulla struct di destinazione usando la Property
|
|
func (nm *NotionMapper[TDest]) ForMember(propertyName string, setter func(prop notionapi.Property, dest *TDest)) *NotionMapper[TDest] {
|
|
nm.propertyMappings[propertyName] = setter
|
|
return nm
|
|
}
|
|
|
|
// ForPage configura un mapping che ha accesso all'intera Page (utile per ID, timestamps, etc.)
|
|
// setter: funzione che setta il valore sulla struct di destinazione usando la Page completa
|
|
func (nm *NotionMapper[TDest]) ForPage(setter func(page notionapi.Page, dest *TDest)) *NotionMapper[TDest] {
|
|
nm.pageMappings = append(nm.pageMappings, setter)
|
|
return nm
|
|
}
|
|
|
|
// MapFrom mappa una Page di Notion alla struct di destinazione
|
|
func (nm *NotionMapper[TDest]) MapFrom(page notionapi.Page) TDest {
|
|
var dest TDest
|
|
|
|
// Applica i mapping delle properties
|
|
for propName, mapping := range nm.propertyMappings {
|
|
if prop, ok := page.Properties[propName]; ok {
|
|
mapping(prop, &dest)
|
|
}
|
|
}
|
|
|
|
// Applica i mapping che richiedono l'intera page
|
|
for _, mapping := range nm.pageMappings {
|
|
mapping(page, &dest)
|
|
}
|
|
|
|
return dest
|
|
}
|
|
|
|
// Helper functions per estrarre valori comuni da Notion properties
|
|
|
|
// ExtractTitle estrae il testo da una TitleProperty
|
|
func ExtractTitle(prop notionapi.Property) string {
|
|
if title, ok := prop.(*notionapi.TitleProperty); ok && len(title.Title) > 0 {
|
|
return title.Title[0].PlainText
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// ExtractRichText estrae il testo da una RichTextProperty
|
|
func ExtractRichText(prop notionapi.Property) string {
|
|
if richText, ok := prop.(*notionapi.RichTextProperty); ok && len(richText.RichText) > 0 {
|
|
return richText.RichText[0].PlainText
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// ExtractNumber estrae un numero da una NumberProperty
|
|
func ExtractNumber(prop notionapi.Property) float64 {
|
|
if num, ok := prop.(*notionapi.NumberProperty); ok {
|
|
return num.Number
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// ExtractSelect estrae il valore da una SelectProperty
|
|
func ExtractSelect(prop notionapi.Property) string {
|
|
if sel, ok := prop.(*notionapi.SelectProperty); ok && sel.Select.Name != "" {
|
|
return sel.Select.Name
|
|
}
|
|
return ""
|
|
}
|