Files
CrimsonandGitHub 538a2d0de2 chore: Refactor models and other shareable packages into importable pkg (#762)
* Move models and other shareable code to pkg/domain and add own go.mod

* Update all references to use pkg/domain paths

* Fix generated file

* Ensure pkg/domain tests are run by just tests

* Use new models path in connector templates

* Copy local pkg/domain in dockerfile

* Ensure pkg/domain is subject to go generate
2026-06-23 13:08:32 +00:00

56 lines
1.4 KiB
Go

package models
import (
"encoding/json"
"time"
)
// since the json unmarshaller is case-insensitive this generic interface will be used to receive back the unmarshaled struct from a plugin
// then we can remarshal the payload and enforce the case we expect (eg. clientID vs clientId) rather than blindly trusting the raw user input
type PluginInternalConfig interface{}
// Config is the generic configuration that all connectors share
// Note that the PollingPeriod defined here is often overwritten by the connector-specific configuration
type Config struct {
Name string `json:"name" validate:"required,gte=3,lte=500"`
PollingPeriod time.Duration `json:"pollingPeriod" validate:"required"`
}
func (c Config) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Name string `json:"name"`
PollingPeriod string `json:"pollingPeriod"`
}{
Name: c.Name,
PollingPeriod: c.PollingPeriod.String(),
})
}
func (c *Config) UnmarshalJSON(data []byte) error {
var raw struct {
Name string `json:"name"`
PollingPeriod string `json:"pollingPeriod"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
var pollingPeriod time.Duration
if raw.PollingPeriod != "" {
p, err := time.ParseDuration(raw.PollingPeriod)
if err != nil {
return err
}
pollingPeriod = p
}
c.Name = raw.Name
if pollingPeriod > 0 {
c.PollingPeriod = pollingPeriod
}
return nil
}