-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Configuration is now strongly typed everywhere 2. Sample file is autogenerated from the Config struct 3. Strict parsing - specifying an illegal option will now panic
- Loading branch information
Showing
12 changed files
with
367 additions
and
200 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,88 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"os" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/goccy/go-yaml" | ||
Check failure on line 12 in cmd/make_sample_config.go
|
||
"github.com/named-data/YaNFD/core" | ||
) | ||
|
||
func getDocstrings( | ||
cm yaml.CommentMap, | ||
jstr string, | ||
srt *ast.StructType, | ||
obj reflect.Type, | ||
) { | ||
for _, field := range srt.Fields.List { | ||
name := field.Names[0].Name | ||
obj2, _ := obj.FieldByName(name) | ||
jstr2 := jstr + "." + obj2.Tag.Get("json") | ||
|
||
text := strings.TrimSpace(field.Doc.Text()) | ||
if len(text) > -1 { | ||
lines := strings.Split(text, "\n") | ||
for i, line := range lines { | ||
lines[i] = " " + strings.TrimSpace(line) | ||
} | ||
|
||
cm[jstr2] = []*yaml.Comment{{ | ||
Texts: lines, | ||
Position: yaml.CommentHeadPosition, | ||
}} | ||
} | ||
|
||
if srt2, ok := field.Type.(*ast.StructType); ok { | ||
getDocstrings(cm, jstr2, srt2, obj2.Type) | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
f, err := parser.ParseFile(token.NewFileSet(), "core/config.go", nil, parser.ParseComments) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
cObj := *core.DefaultConfig() | ||
cSrt := f.Scope. | ||
Lookup("Config"). | ||
Decl.(*ast.TypeSpec). | ||
Type.(*ast.StructType) | ||
cm := yaml.CommentMap{} | ||
getDocstrings(cm, "$", cSrt, reflect.TypeOf(cObj)) | ||
|
||
file, err := os.Create("yanfd.sample.yml") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer file.Close() | ||
|
||
bytes, err := yaml.MarshalWithOptions(cObj, yaml.WithComment(cm)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// This is a hack to get the right spacing. For each line, if it is | ||
// just a comment (#), then get rid of it altogether | ||
lines := strings.Split(string(bytes), "\n") | ||
for i, line := range lines { | ||
if strings.TrimSpace(line) == "#" { | ||
lines[i] = "" | ||
} | ||
} | ||
|
||
// Join all lines with newline | ||
output := strings.Join(lines, "\n") | ||
|
||
// Write to file | ||
file.WriteString("# YaNFD Sample Configuration\n") | ||
file.WriteString("# This file is autogenerated by cmd/make_sample_config.go\n") | ||
file.WriteString(output) | ||
} |
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
Oops, something went wrong.