Skip to content

Commit

Permalink
Use default value in config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
CapacitorSet committed Aug 12, 2018
1 parent 2071c4d commit 66634b0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
75 changes: 74 additions & 1 deletion scripts/gen-service-docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ import (
"bytes"
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"reflect"
"strings"

"github.com/BurntSushi/toml"
"strconv"
)

type service struct {
Name string `toml:"name"`
Description string `toml:"description"`
Config string `toml:"config"`
Config string /* `toml:"config"` */
Filename string
}

Expand Down Expand Up @@ -84,6 +87,76 @@ parsing:
if len(md.Undecoded()) != 0 {
fmt.Fprintf(os.Stderr, "Unrecognized keys: %v\n", md.Undecoded())
}

configMap := make(map[string]interface{})
var configStruct *ast.StructType
for _, _decl := range file.Decls {
decl, ok := _decl.(*ast.GenDecl)
if !ok {
continue
}
if decl.Tok != token.TYPE {
continue
}
typeSpec := decl.Specs[0].(*ast.TypeSpec)
if !strings.HasSuffix(typeSpec.Name.Name, "Config") {
continue
}
fmt.Printf("Inspecting struct %s.\n", typeSpec.Name.Name)
configStruct, ok = typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
}
if configStruct == nil {
panic("No config struct found!")
}
for _, field := range configStruct.Fields.List {
if field.Tag == nil {
continue
}
if len(field.Names) != 1 {
panic("Not supported")
}
if field.Type.(*ast.Ident).Name != "string" {
panic("Not supported")
}
name := field.Names[0].Name
tag := reflect.StructTag(strings.Replace(field.Tag.Value, "`", "", -1))
tomlTag, tomlOk := tag.Lookup("toml")
defaultTag, defaultOk := tag.Lookup("default")
if !tomlOk {
if !defaultOk {
continue
} else {
panic("Field " + name + "has a default tag, but no toml tag")
}
}
if !defaultOk {
panic("No default tag for field " + name)
continue
}
switch field.Type.(*ast.Ident).Name {
case "string":
configMap[tomlTag] = defaultTag
case "int":
val, err := strconv.Atoi(defaultTag)
if err != nil {
panic(err)
}
configMap[tomlTag] = val
default:
panic("Type " + field.Type.(*ast.Ident).Name + "is not supported")
}
}

buf := new(bytes.Buffer)
err = toml.NewEncoder(buf).Encode(configMap)
if err != nil {
panic(err)
}
svc.Config = buf.String()

tmp := struct{}{}
_, err = toml.Decode(svc.Config, &tmp)
if err != nil {
Expand Down
8 changes: 2 additions & 6 deletions services/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
/* Metadata:
name = "redis"
description = "The Redis service will simulate a redis server. Currently the INFO command has been implemented."
config = """
version="4.0.6"
os="Linux 4.9.49-moby x86_64"
"""
*/
package redis

Expand Down Expand Up @@ -72,9 +68,9 @@ func REDIS(options ...services.ServicerFunc) services.Servicer {
}

type redisServiceConfig struct {
Version string `toml:"version"`
Version string `toml:"version" default:"4.0.6"`

Os string `toml:"os"`
Os string `toml:"os" default:"Linux 4.9.49-moby x86_64"`
}

type redisService struct {
Expand Down

0 comments on commit 66634b0

Please sign in to comment.