comparison pkg/imports/config.go @ 2016:25967829cf00 unify_imports

Started to simplify the import models.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 24 Jan 2019 17:14:10 +0100
parents 59055c8301df
children d29ac997eb34
comparison
equal deleted inserted replaced
2003:917c672591c2 2016:25967829cf00
17 "context" 17 "context"
18 "database/sql" 18 "database/sql"
19 "encoding/json" 19 "encoding/json"
20 "fmt" 20 "fmt"
21 21
22 "github.com/robfig/cron"
23
24 "gemma.intevation.de/gemma/pkg/auth" 22 "gemma.intevation.de/gemma/pkg/auth"
25 "gemma.intevation.de/gemma/pkg/common" 23 "gemma.intevation.de/gemma/pkg/common"
24 "gemma.intevation.de/gemma/pkg/models"
26 ) 25 )
27 26
28 type ( 27 type (
29 // CronSpec is a string containing a cron line.
30 CronSpec string
31
32 // ImportKind is a string which has to be one 28 // ImportKind is a string which has to be one
33 // of the registered import types. 29 // of the registered import types.
34 ImportKind string 30 ImportKind string
35 31
36 // Config is JSON serialized form of a import configuration. 32 // Config is JSON serialized form of a import configuration.
42 // 'pending' or 'failed'. 38 // 'pending' or 'failed'.
43 SendEMail bool `json:"send-email"` 39 SendEMail bool `json:"send-email"`
44 // Cron is the cron schedule 40 // Cron is the cron schedule
45 // of this configuration if this value is not 41 // of this configuration if this value is not
46 // nil. If nil the import is not scheduled. 42 // nil. If nil the import is not scheduled.
47 Cron *CronSpec `json:"cron"` 43 Cron *models.CronSpec `json:"cron"`
48 // URL is an optional URL used by the import. 44 // URL is an optional URL used by the import.
49 URL *string `json:"url"` 45 URL *string `json:"url"`
50 // Attributes are optional key/value pairs for a configuration. 46 // Attributes are optional key/value pairs for a configuration.
51 Attributes common.Attributes `json:"attributes,omitempty"` 47 Attributes common.Attributes `json:"attributes,omitempty"`
52 } 48 }
56 IDConfig struct { 52 IDConfig struct {
57 ID int64 `json:"id"` 53 ID int64 `json:"id"`
58 User string `json:"user"` 54 User string `json:"user"`
59 Kind ImportKind `json:"kind"` 55 Kind ImportKind `json:"kind"`
60 SendEMail bool `json:"send-email"` 56 SendEMail bool `json:"send-email"`
61 Cron *CronSpec `json:"cron,omitempty"` 57 Cron *models.CronSpec `json:"cron,omitempty"`
62 URL *string `json:"url,omitempty"` 58 URL *string `json:"url,omitempty"`
63 Attributes common.Attributes `json:"attributes,omitempty"` 59 Attributes common.Attributes `json:"attributes,omitempty"`
64 } 60 }
65 ) 61 )
66 62
75 if !HasImportKindName(s) { 71 if !HasImportKindName(s) {
76 return fmt.Errorf("Unknown kind '%s'", s) 72 return fmt.Errorf("Unknown kind '%s'", s)
77 } 73 }
78 74
79 *ik = ImportKind(s) 75 *ik = ImportKind(s)
80
81 return nil
82 }
83
84 // UnmarshalJSON checks if the incoming string is
85 // a valid cron line.
86 func (cs *CronSpec) UnmarshalJSON(data []byte) error {
87 var spec string
88 if err := json.Unmarshal(data, &spec); err != nil {
89 return err
90 }
91 if _, err := cron.Parse(spec); err != nil {
92 return err
93 }
94 *cs = CronSpec(spec)
95 76
96 return nil 77 return nil
97 } 78 }
98 79
99 const ( 80 const (
136 return nil, err 117 return nil, err
137 } 118 }
138 119
139 cfg.Kind = ImportKind(kind) 120 cfg.Kind = ImportKind(kind)
140 if cron.Valid { 121 if cron.Valid {
141 c := CronSpec(cron.String) 122 c := models.CronSpec(cron.String)
142 cfg.Cron = &c 123 cfg.Cron = &c
143 } 124 }
144 if url.Valid { 125 if url.Valid {
145 cfg.URL = &url.String 126 cfg.URL = &url.String
146 } 127 }