forked from bwplotka/mdox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Saswata Mukherjee <[email protected]>
- Loading branch information
1 parent
db5c373
commit d68d51d
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) Bartłomiej Płotka @bwplotka | ||
// Licensed under the Apache License 2.0. | ||
|
||
// Taken from Thanos project. | ||
// | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package yamlgen | ||
|
||
import ( | ||
"io" | ||
"reflect" | ||
|
||
"github.com/fatih/structtag" | ||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func Generate(obj interface{}, w io.Writer) error { | ||
// We forbid omitempty option. This is for simplification for doc generation. | ||
if err := checkForOmitEmptyTagOption(obj); err != nil { | ||
return errors.Wrap(err, "invalid type") | ||
} | ||
return yaml.NewEncoder(w).Encode(obj) | ||
} | ||
|
||
func checkForOmitEmptyTagOption(obj interface{}) error { | ||
return checkForOmitEmptyTagOptionRec(reflect.ValueOf(obj)) | ||
} | ||
|
||
func checkForOmitEmptyTagOptionRec(v reflect.Value) error { | ||
switch v.Kind() { | ||
case reflect.Struct: | ||
for i := 0; i < v.NumField(); i++ { | ||
tags, err := structtag.Parse(string(v.Type().Field(i).Tag)) | ||
if err != nil { | ||
return errors.Wrapf(err, "%s: failed to parse tag %q", v.Type().Field(i).Name, v.Type().Field(i).Tag) | ||
} | ||
|
||
tag, err := tags.Get("yaml") | ||
if err != nil { | ||
return errors.Wrapf(err, "%s: failed to get tag %q", v.Type().Field(i).Name, v.Type().Field(i).Tag) | ||
} | ||
|
||
for _, opts := range tag.Options { | ||
if opts == "omitempty" { | ||
return errors.Errorf("omitempty is forbidden for config, but spotted on field '%s'", v.Type().Field(i).Name) | ||
} | ||
} | ||
|
||
if err := checkForOmitEmptyTagOptionRec(v.Field(i)); err != nil { | ||
return errors.Wrapf(err, "%s", v.Type().Field(i).Name) | ||
} | ||
} | ||
|
||
case reflect.Ptr: | ||
return errors.New("nil pointers are not allowed in configuration") | ||
|
||
case reflect.Interface: | ||
return checkForOmitEmptyTagOptionRec(v.Elem()) | ||
} | ||
|
||
return nil | ||
} |