Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Aug 8, 2024
1 parent 316077e commit a7c156e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 80 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func TestConvertToGoStructName(t *testing.T) {
name string
want string
}{
{"config_center", "ConfigCenter"},
{"config_center_test", "ConfigCenterTest"},
{"config-center", "ConfigCenter"},
{"config_center.yml", "ConfigCenter"},
{"config-center_test", "ConfigCenterTest"},
{"dss/config-center", "ConfigCenter"},
{"config-center-test", "ConfigCenterTest"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ConvertToGoStructName(tt.name); got != tt.want {
t.Errorf("ConvertToGoStructName() = %v, want %v", got, tt.want)
if got := convertToGoStructName(tt.name); got != tt.want {
t.Errorf("convertToGoStructName() = %v, want %v", got, tt.want)
}
})
}
Expand Down
24 changes: 13 additions & 11 deletions pkg/config_generator/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@

package config_generator

const (
Yaml = "yaml"
Json = "json"
Text = "text"
)

type ConfigGenerateMeta struct {
Desc string `json:"kind,omitempty"`
Kind string `json:"group,omitempty"`
ConfigStruct Struct `json:"config_struct,omitempty"`
FileType string `json:"file_type,omitempty"`
Key string `json:"key,omitempty"`
// When not support file type, use raw_text to store the content
// RawText is nil when support file type
RawText string `json:"raw_text,omitempty"`
Desc string `json:"desc,omitempty"`
Kind string `json:"kind,omitempty"`
ConfigStruct Struct `json:"config_struct,omitempty"`
ConfigValueType string `json:"config_value_type,omitempty"`
Key string `json:"key,omitempty"`

structTree map[string]Struct `json:"struct_tree"`
structTree map[string]Struct `json:"-"`
}

type FieldTag struct {
Expand All @@ -38,13 +41,12 @@ type FieldTag struct {
type Struct struct {
StructName string `json:"struct_name,omitempty"`
Fields []Field `json:"fields,omitempty"`
Type string `json:"type,omitempty"`
}

type Field struct {
FieldName string `json:"field_name,omitempty"` // FieldName of the field
Value string `json:"value,omitempty"` // Value of the field (used for primitive types)
Type string `json:"type,omitempty"` // Type of the field
FieldType string `json:"field_type,omitempty"` // FieldType of the field
IsStruct bool `json:"is_struct"`
IsSlice bool `json:"is_slice"`
IsBasicType bool `json:"is_basic"`
Expand Down
27 changes: 11 additions & 16 deletions pkg/config_generator/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package config_generator
import "fmt"

type Result struct {
ConfigName string `json:"config_name,omitempty"`
ServiceName string `json:"service_name,omitempty"`
Addr string `json:"addr,omitempty"`
SubConfigMetadataList []SubConfigMetadata `json:"sub_config_metadata_list,omitempty"`
}
Expand All @@ -40,8 +40,8 @@ func HandleRequest(req *Config) (*Result, error) {

// Create a new result object
result := &Result{
ConfigName: req.ConfigName,
Addr: addr,
ServiceName: req.ServiceName,
Addr: addr,
}

// Iterate over each sub-config and process it
Expand Down Expand Up @@ -93,7 +93,7 @@ func processConfigKvPair(configKvPair *ConfigKvPair) ([]ConfigGenerateMeta, erro
case ConfigValueType_YamlType, ConfigValueType_JsonType:
// Convert configuration content into Go structs
yaml2Go := New(key, desc, group, asFileType(valueType))
if _, err := yaml2Go.Convert(ConvertToGoStructName(key), []byte(content)); err != nil {
if _, err := yaml2Go.Convert(convertToGoStructName(key), []byte(content)); err != nil {
return nil, fmt.Errorf("failed to convert content for key '%s': %w", key, err)
}

Expand All @@ -103,11 +103,10 @@ func processConfigKvPair(configKvPair *ConfigKvPair) ([]ConfigGenerateMeta, erro
default:
// Store the raw text content for unsupported types
result = append(result, ConfigGenerateMeta{
Desc: desc,
Kind: group,
FileType: asFileType(valueType),
RawText: content,
Key: key,
Desc: desc,
Kind: group,
ConfigValueType: asFileType(valueType),
Key: key,
})
}

Expand All @@ -118,14 +117,10 @@ func processConfigKvPair(configKvPair *ConfigKvPair) ([]ConfigGenerateMeta, erro
func asFileType(t ConfigValueType) string {
switch t {
case ConfigValueType_YamlType:
return "yaml"
case ConfigValueType_StringType:
return "string"
return Yaml
case ConfigValueType_JsonType:
return "json"
case ConfigValueType_XmlType:
return "xml"
return Json
default:
return "unknown"
return Text
}
}
2 changes: 1 addition & 1 deletion pkg/config_generator/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func Test_HandleRequest(t *testing.T) {
c := &Config{
ConfigName: "nacos_config_server",
ServiceName: "nacos_config_server",
SubConfigList: []*SubConfig{
{
NameSpace: "public",
Expand Down
3 changes: 1 addition & 2 deletions pkg/config_generator/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
package config_generator

import (
_ "embed"
"path/filepath"
"strings"
)

func ConvertToGoStructName(s string) string {
func convertToGoStructName(s string) string {
var result string
s = filepath.Base(s)
if idx := strings.LastIndex(s, "."); idx != -1 {
Expand Down
26 changes: 13 additions & 13 deletions pkg/config_generator/yaml2go.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import (
func New(key, desc, kind, fileType string) Yaml2Go {
return Yaml2Go{
StructsMeta: &ConfigGenerateMeta{
Key: key,
Kind: kind,
Desc: desc,
FileType: fileType,
structTree: make(map[string]Struct),
Key: key,
Kind: kind,
Desc: desc,
ConfigValueType: fileType,
structTree: make(map[string]Struct),
},
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func goKeyFormat(key string) string {

// Convert transforms map[string]interface{} to go struct
func (yg *Yaml2Go) Convert(structName string, data []byte) (string, error) {
structName = ConvertToGoStructName(structName)
structName = convertToGoStructName(structName)
yg.visited = make(map[line]bool)
yg.structMap = make(map[string]string)

Expand Down Expand Up @@ -141,7 +141,7 @@ func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool
Fields: []Field{
{
FieldName: goKeyFormat(k),
Type: "interface{}",
FieldType: "interface{}",
Tags: []FieldTag{
{
TagKey: "yaml",
Expand Down Expand Up @@ -174,7 +174,7 @@ func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool
Fields: []Field{
{
FieldName: key,
Type: newKey,
FieldType: newKey,
IsStruct: true,
Tags: []FieldTag{
{
Expand Down Expand Up @@ -214,7 +214,7 @@ func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool
Fields: []Field{
{
FieldName: goKeyFormat(k),
Type: fmt.Sprintf("[]%s", reflect.TypeOf(val[0])),
FieldType: fmt.Sprintf("[]%s", reflect.TypeOf(val[0])),
IsStruct: false,
IsSlice: true,
Tags: []FieldTag{
Expand Down Expand Up @@ -242,7 +242,7 @@ func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool
}
structOr.Fields[0].Children = append(structOr.Fields[0].Children, Field{
Value: v1,
Type: reflect.TypeOf(v).String(),
FieldType: reflect.TypeOf(v).String(),
IsBasicType: true,
})
}
Expand All @@ -258,7 +258,7 @@ func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool
Fields: []Field{
{
FieldName: key,
Type: fmt.Sprintf("[]%s", newKey),
FieldType: fmt.Sprintf("[]%s", newKey),
IsSlice: true,
Tags: []FieldTag{
{
Expand All @@ -285,7 +285,7 @@ func (yg *Yaml2Go) Structify(structName, k string, v interface{}, arrayElem bool
Fields: []Field{
{
FieldName: goKeyFormat(k),
Type: reflect.TypeOf(v).String(),
FieldType: reflect.TypeOf(v).String(),
IsBasicType: isBasicType(reflect.TypeOf(v).String()),
Tags: []FieldTag{
{
Expand Down Expand Up @@ -345,7 +345,7 @@ func organizeStructHelper(currentStruct Struct, structs map[string]Struct, visit
// Iterate over each field in the struct
for i, field := range currentStruct.Fields {
// Handle array types by stripping brackets and checking the type
fieldType := field.Type
fieldType := field.FieldType
isSlice := false
if strings.HasPrefix(fieldType, "[]") {
fieldType = strings.TrimPrefix(fieldType, "[]")
Expand Down
14 changes: 0 additions & 14 deletions pkg/config_generator/yaml2go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,4 @@ func TestYaml2Go(t *testing.T) {
t.Fatal(err)
}
os.WriteFile("metadata.json", marshal, 0o644)

ss := yaml2Go.StructsMeta.ConfigStruct
if ss.StructName != "Config" {
t.Fatalf("expect Config, got %s", ss.StructName)
}
if len(ss.Fields) != 1 {
t.Fatalf("expect 1, got %d", len(ss.Fields))
}
if ss.Fields[0].FieldName != "Kitex" {
t.Fatalf("expect Kitex, got %s", ss.Fields[0].FieldName)
}
if ss.Fields[0].IsStruct != true {
t.Fatalf("expect true, got %v", ss.Fields[0].IsStruct)
}
}

0 comments on commit a7c156e

Please sign in to comment.