2023-06-16 15:38:28 +00:00
|
|
|
package sources
|
|
|
|
|
2023-06-23 16:15:51 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure CommonSourceUnit implements SourceUnit at compile time.
|
|
|
|
var _ SourceUnit = CommonSourceUnit{}
|
|
|
|
|
2023-06-16 15:38:28 +00:00
|
|
|
// CommonSourceUnit is a common implementation of SourceUnit that Sources can
|
|
|
|
// use instead of implementing their own types.
|
|
|
|
type CommonSourceUnit struct {
|
|
|
|
ID string `json:"source_unit_id"`
|
|
|
|
}
|
|
|
|
|
2023-06-23 16:15:51 +00:00
|
|
|
// SourceUnitID implements the SourceUnit interface.
|
2023-06-16 15:38:28 +00:00
|
|
|
func (c CommonSourceUnit) SourceUnitID() string {
|
|
|
|
return c.ID
|
|
|
|
}
|
2023-06-23 16:15:51 +00:00
|
|
|
|
|
|
|
// CommonSourceUnitUnmarshaller is an implementation of SourceUnitUnmarshaller
|
|
|
|
// for the CommonSourceUnit. A source can embed this struct to gain the
|
|
|
|
// functionality of converting []byte to a CommonSourceUnit.
|
|
|
|
type CommonSourceUnitUnmarshaller struct{}
|
|
|
|
|
|
|
|
// UnmarshalSourceUnit implements the SourceUnitUnmarshaller interface.
|
|
|
|
func (c CommonSourceUnitUnmarshaller) UnmarshalSourceUnit(data []byte) (SourceUnit, error) {
|
|
|
|
var unit CommonSourceUnit
|
|
|
|
if err := json.Unmarshal(data, &unit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if unit.ID == "" {
|
|
|
|
return nil, fmt.Errorf("not a CommonSourceUnit")
|
|
|
|
}
|
|
|
|
return unit, nil
|
|
|
|
}
|