Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
tidy up readme generation
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Nov 1, 2019
1 parent ca68320 commit f0c392a
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 52 deletions.
58 changes: 43 additions & 15 deletions EXPERIMENTAL/typicmd/buildtool/readme.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package buildtool

import (
"fmt"
"io"

"github.com/iancoleman/strcase"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typicmd/buildtool/markdown"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj"
)

const (
Expand All @@ -22,11 +25,17 @@ type Readme struct {
func (r Readme) Markdown(w io.Writer) *markdown.Markdown {
md := &markdown.Markdown{Writer: w}
md.Comment("Autogenerated by Typical-Go. DO NOT EDIT.")
md.Heading1(r.Name)
md.Writeln(r.Description)
if r.Name != "" {
md.Heading1(r.Name)
} else {
md.Heading1("Typical-Go Project")
}
if r.Description != "" {
md.Writeln(r.Description)
}
r.prerequisite(md)
r.runInstruction(md)
// r.configuration(md)
r.configuration(md)
r.releaseDistribution(md)
return md
}
Expand All @@ -49,15 +58,34 @@ func (r Readme) releaseDistribution(md *markdown.Markdown) (err error) {
return
}

// func (r Readme) configuration(md *markdown.Markdown) {
// md.Heading2("Configuration")
// for _, help := range r.Modules.Helps() {
// if module.Name != "" {
// md.Heading3(strcase.ToCamel(module.Name))
// }
// var builder strings.Builder
// cfg := module.Config
// envconfig.Usagef(cfg.Prefix(), cfg.Spec(), &builder, configTemplate)
// md.Writeln(builder.String())
// }
// }
func (r Readme) configuration(md *markdown.Markdown) {
md.Heading2("Configurations")
if configurer, ok := r.Application.(typiobj.Configurer); ok {
configTable(md, configurer.Configure().ConfigFields())
}
for _, module := range r.Modules {
if name := typiobj.Name(module); name != "" {
md.Heading3(strcase.ToCamel(name))
}
if description := typiobj.Description(module); description != "" {
md.Writeln(description)
}
if configurer, ok := module.(typiobj.Configurer); ok {
configTable(md, configurer.Configure().ConfigFields())
}
}
}

func configTable(md *markdown.Markdown, fields []typiobj.ConfigField) {
md.WriteString("| Name | Type | Default | Required |\n")
md.WriteString("|---|---|---|---|\n")
for _, field := range fields {
var required string
if field.Required {
required = "Yes"
}
md.WriteString(fmt.Sprintf("|%s|%s|%s|%s|\n",
field.Name, field.Type, field.Default, required))
}
md.WriteString("\n")
}
27 changes: 27 additions & 0 deletions EXPERIMENTAL/typiobj/attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package typiobj

import "reflect"

// Name of object. Return value from name field if available or return its type.
func Name(obj interface{}) (name string) {
val := reflect.Indirect(reflect.ValueOf(obj)).FieldByName("Name")
name = val.String()
if name == "" || name == "<invalid Value>" {
typ := reflect.TypeOf(obj)
name = typ.Name()
if name == "" {
name = typ.String()
}
}
return
}

// Description of Object. Return value from description field if available or return its type
func Description(obj interface{}) (description string) {
val := reflect.Indirect(reflect.ValueOf(obj)).FieldByName("Description")
description = val.String()
if description == "<invalid Value>" {
description = ""
}
return
}
63 changes: 63 additions & 0 deletions EXPERIMENTAL/typiobj/attribute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package typiobj_test

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj"
)

type SampleAttribute struct {
Name string
Description string
}

func TestName(t *testing.T) {
testcases := []struct {
obj interface{}
name string
}{
{
obj: SampleAttribute{
Name: "some-name",
},
name: "some-name",
},
{
obj: SampleAttribute{},
name: "SampleAttribute",
},
{
obj: struct{}{},
name: "struct {}",
},
}
for _, tt := range testcases {
require.Equal(t, tt.name, typiobj.Name(tt.obj))
}
}

func TestDescription(t *testing.T) {
testcases := []struct {
obj interface{}
description string
}{
{
obj: SampleAttribute{
Description: "some-description",
},
description: "some-description",
},
{
obj: SampleAttribute{},
description: "",
},
{
obj: struct{}{},
description: "",
},
}
for _, tt := range testcases {
require.Equal(t, tt.description, typiobj.Description(tt.obj))
}
}
13 changes: 0 additions & 13 deletions EXPERIMENTAL/typiobj/field.go

This file was deleted.

52 changes: 28 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,42 @@ Example of typical and scalable RESTful API Server for Go

Use `./typicalw run` to compile and run local development. [Learn more](https://typical-go.github.io/learn-more/wrapper.html)

## Configuration
## Configurations

| Name | Type | Default | Required |
|---|---|---|---|
|APP_ADDRESS|string|:8089|Yes|

### Server

| Key | Type | Default | Required | Description |
|---|---|---|---|---|
|SERVER_DEBUG|True or False|false|||
| Name | Type | Default | Required |
|---|---|---|---|
|SERVER_DEBUG|bool|false||

### PostgresDatabase
### Postgres

| Key | Type | Default | Required | Description |
|---|---|---|---|---|
|PG_DBNAME|String||true||
|PG_USER|String|postgres|true||
|PG_PASSWORD|String|pgpass|true||
|PG_HOST|String|localhost|||
|PG_PORT|Integer|5432|||
| Name | Type | Default | Required |
|---|---|---|---|
|PG_DBNAME|string||Yes|
|PG_USER|string|postgres|Yes|
|PG_PASSWORD|string|pgpass|Yes|
|PG_HOST|string|localhost||
|PG_PORT|int|5432||

### Redis

| Key | Type | Default | Required | Description |
|---|---|---|---|---|
|REDIS_HOST|String|localhost|true||
|REDIS_PORT|String|6379|true||
|REDIS_PASSWORD|String|redispass|||
|REDIS_DB|Integer|0|||
|REDIS_POOL_SIZE|Integer|20|true||
|REDIS_DIAL_TIMEOUT|Duration|5s|true||
|REDIS_READ_WRITE_TIMEOUT|Duration|3s|true||
|REDIS_IDLE_TIMEOUT|Duration|5m|true||
|REDIS_IDLE_CHECK_FREQUENCY|Duration|1m|true||
|REDIS_MAX_CONN_AGE|Duration|30m|true||
| Name | Type | Default | Required |
|---|---|---|---|
|REDIS_HOST|string|localhost|Yes|
|REDIS_PORT|string|6379|Yes|
|REDIS_PASSWORD|string|redispass||
|REDIS_DB|int|0||
|REDIS_POOL_SIZE|int|20|Yes|
|REDIS_DIAL_TIMEOUT|Duration|5s|Yes|
|REDIS_READ_WRITE_TIMEOUT|Duration|3s|Yes|
|REDIS_IDLE_TIMEOUT|Duration|5m|Yes|
|REDIS_IDLE_CHECK_FREQUENCY|Duration|1m|Yes|
|REDIS_MAX_CONN_AGE|Duration|30m|Yes|

## Release Distribution

Expand Down

0 comments on commit f0c392a

Please sign in to comment.