feat: jsonnet interface

This commit is contained in:
sh0rez 2020-03-25 16:20:51 +01:00
parent 5977373772
commit a63ddd1061
No known key found for this signature in database
GPG key ID: 87C71DF9F8181FF1
5 changed files with 166 additions and 83 deletions

24
main.go
View file

@ -13,7 +13,7 @@ type Doc struct {
Import string `json:"import"`
Help string `json:"help"`
API Object `json:"api"`
API Fields `json:"api"`
}
func main() {
@ -78,6 +78,28 @@ type Field struct {
Value *Value `json:"value,omitempty"`
}
func (o *Field) UnmarshalJSON(data []byte) error {
type fake Field
var f fake
if err := json.Unmarshal(data, &f); err != nil {
return err
}
switch {
case f.Function != nil:
o.Function = f.Function
case f.Object != nil:
o.Object = f.Object
case f.Value != nil:
o.Value = f.Value
default:
return errors.New("field has no value")
}
return nil
}
func (o Field) MarshalJSON() ([]byte, error) {
if o.Function == nil && o.Object == nil && o.Value == nil {
return nil, errors.New("field has no value")