sriracha/extension.go

68 lines
1.2 KiB
Go

package sriracha
import (
"io"
"log"
"reflect"
"strings"
)
type Extension interface {
Description() string
}
var (
allExtensions []Extension
allExtensionNames []string
)
func AddExtension(e Extension) {
for _, ext := range allExtensions {
if ext == e {
return
}
}
allExtensions = append(allExtensions, e)
allExtensionNames = append(allExtensionNames, extensionName(e))
}
func extensionName(e Extension) string {
name := reflect.ValueOf(e).Type().String()
if name[0] == '*' {
name = name[1:]
}
if strings.HasPrefix(name, "extension.") {
name = name[10:]
}
return strings.ToUpper(name[0:1]) + name[1:]
}
func attach(file io.Reader, size int64, mime string) (*Attachment, error) {
for i, ext := range allExtensions {
if extension, ok := ext.(ExtensionAttach); ok {
log.Println("DEBUG ATTACH", allExtensionNames[i])
a, err := extension.Attach(file, size, mime)
if err != nil {
return nil, err
} else if a != nil {
return a, nil
}
}
}
return nil, nil
}
func post(p *Post) error {
for i, ext := range allExtensions {
if extension, ok := ext.(ExtensionPost); ok {
log.Println("DEBUG POST", allExtensionNames[i])
err := extension.Post(p)
if err != nil {
return err
}
}
}
return nil
}