-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* ✨ feat: proto to kcl_schema Signed-off-by: xiaok29 <1526783667@qq.com> * fix:add bool float type Signed-off-by: xiaok29 <1526783667@qq.com> * fix:line breaks on different platforms Signed-off-by: xiaok29 <1526783667@qq.com> * fix:line breaks on different platforms Signed-off-by: xiaok29 <1526783667@qq.com> * perf:use writeString splicing Signed-off-by: xiaok29 <1526783667@qq.com> * Update pkg/tools/gen/genkcl_proto.go Co-authored-by: Peefy <xpf6677@163.com> Signed-off-by: xiaok29 <1526783667@qq.com> * feat:read enum type Signed-off-by: xiaok29 <1526783667@qq.com> --------- Signed-off-by: xiaok29 <1526783667@qq.com> Co-authored-by: Peefy <xpf6677@163.com>
- Loading branch information
Showing
8 changed files
with
328 additions
and
10 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
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,161 @@ | ||
package gen | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/emicklei/proto" | ||
) | ||
|
||
var defaultFieldTypeMap = map[string]string{ | ||
"uint32": "int", | ||
"uint64": "int", | ||
"int32": "int", | ||
"int64": "int", | ||
"sint32": "int", | ||
"sint64": "int", | ||
"string": "str", | ||
"google.protobuf.Any": "any", | ||
"bool": "bool", | ||
"float": "float", | ||
"double": "float", | ||
} | ||
|
||
// genKclFromProtoData | ||
func (k *kclGenerator) genKclFromProtoData(w io.Writer, filename string, src interface{}) error { | ||
lineBreak := "\n" | ||
if runtime.GOOS == "windows" { | ||
lineBreak = "\r\n" | ||
} | ||
|
||
code, err := readSource(filename, src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
parser := proto.NewParser(bytes.NewBuffer(code)) | ||
definitions, err := parser.Parse() | ||
if err != nil { | ||
return fmt.Errorf(`error parsing proto file %v: %v`, filename, err) | ||
} | ||
|
||
fieldTypeMap := k.genFieldTypeMap(definitions) | ||
builder := bufio.NewWriter(w) | ||
for _, definition := range definitions.Elements { | ||
message, ok := definition.(*proto.Message) | ||
if !ok { | ||
continue | ||
} | ||
|
||
builder.WriteString("schema ") | ||
builder.WriteString(message.Name) | ||
builder.WriteString(":") | ||
builder.WriteString(lineBreak) | ||
|
||
for _, element := range message.Elements { | ||
switch field := element.(type) { | ||
case *proto.NormalField: | ||
builder.WriteString(" ") | ||
builder.WriteString(field.Name) | ||
if field.Optional { | ||
builder.WriteString("?") | ||
} | ||
builder.WriteString(": ") | ||
|
||
if field.Repeated { | ||
builder.WriteString("[") | ||
} | ||
|
||
fieldType, err := getFieldType(fieldTypeMap, field.Type) | ||
if err != nil { | ||
return err | ||
} | ||
builder.WriteString(fieldType) | ||
|
||
if field.Repeated { | ||
builder.WriteString("]") | ||
} | ||
builder.WriteString(lineBreak) | ||
|
||
case *proto.MapField: | ||
builder.WriteString(" ") | ||
builder.WriteString(field.Name) | ||
builder.WriteString(": {") | ||
keyType, err := getFieldType(fieldTypeMap, field.KeyType) | ||
if err != nil { | ||
return err | ||
} | ||
builder.WriteString(keyType) | ||
builder.WriteString(":") | ||
fieldType, err := getFieldType(fieldTypeMap, field.Type) | ||
if err != nil { | ||
return err | ||
} | ||
builder.WriteString(fieldType) | ||
builder.WriteString("}") | ||
builder.WriteString(lineBreak) | ||
} | ||
} | ||
|
||
builder.WriteString(lineBreak) | ||
} | ||
|
||
if err = builder.Flush(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// GenFieldTypeMap | ||
func (k *kclGenerator) genFieldTypeMap(definitions *proto.Proto) map[string]string { | ||
fieldTypeMap := make(map[string]string) | ||
for key, value := range defaultFieldTypeMap { | ||
fieldTypeMap[key] = value | ||
} | ||
|
||
for _, definition := range definitions.Elements { | ||
switch visitee := definition.(type) { | ||
case *proto.Message: | ||
fieldTypeMap[visitee.Name] = visitee.Name | ||
case *proto.Enum: | ||
var builder strings.Builder | ||
elementsLen := len(visitee.Elements) - 1 | ||
for i, e := range visitee.Elements { | ||
v, ok := e.(*proto.EnumField) | ||
if !ok { | ||
continue | ||
} | ||
|
||
value := fmt.Sprintf(`"%v"`, v.Name) | ||
if k.opts.UseIntegersForNumbers { | ||
value = strconv.Itoa(v.Integer) | ||
} | ||
|
||
builder.WriteString(value) | ||
if elementsLen > i { | ||
builder.WriteString(` | `) | ||
} | ||
|
||
fieldTypeMap[v.Name] = v.Name | ||
} | ||
fieldTypeMap[visitee.Name] = builder.String() | ||
} | ||
} | ||
|
||
return fieldTypeMap | ||
} | ||
|
||
func getFieldType(fieldTypeMap map[string]string, fieldType string) (string, error) { | ||
value, ok := fieldTypeMap[fieldType] | ||
if !ok { | ||
return "", fmt.Errorf(`this "%v" is not currently supported`, fieldType) | ||
} | ||
|
||
return value, nil | ||
} |
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,36 @@ | ||
schema Person: | ||
name: str | ||
age: int | ||
friends: [str] | ||
movies: {str:Movie} | ||
employee?: employee | ||
company: Company | ||
star_int?: int | ||
star_map: {str:str} | ||
inter: any | ||
|
||
schema Movie: | ||
desc: str | ||
size: int | ||
kind: str | ||
unknown1: any | ||
unknown2: any | ||
|
||
schema employee: | ||
name: str | ||
age: int | ||
friends: [str] | ||
movies: {str:Movie} | ||
bank_card: int | ||
nationality: str | ||
salary: float | ||
age_double: float | ||
is_married: bool | ||
gender: "unknown" | "male" | "female" | ||
gender_opt?: "unknown" | "male" | "female" | ||
|
||
schema Company: | ||
name: str | ||
employees: [employee] | ||
persons?: Person | ||
|
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,54 @@ | ||
syntax = "proto3"; | ||
package test; | ||
option go_package = "test"; | ||
|
||
import "google/protobuf/any.proto"; | ||
|
||
|
||
message Person{ | ||
string name = 1; | ||
int64 age = 2; | ||
repeated string friends = 3; | ||
map<string,Movie> movies = 4; | ||
optional employee employee = 6; | ||
Company company = 7; | ||
optional int32 star_int = 8; | ||
map<string,string> star_map = 9; | ||
google.protobuf.Any inter = 10; | ||
} | ||
|
||
message Movie{ | ||
string desc = 1; | ||
int32 size = 2; | ||
string kind = 3; | ||
google.protobuf.Any unknown1 = 4; | ||
google.protobuf.Any unknown2 = 5; | ||
} | ||
|
||
|
||
message employee{ | ||
string name = 1; | ||
uint32 age = 2; | ||
repeated string friends = 3; | ||
map<string,Movie> movies = 4; | ||
int32 bank_card = 5; | ||
string nationality = 6; | ||
float salary = 7; | ||
double age_double = 8; | ||
bool is_married = 9; | ||
gender gender = 10; | ||
optional gender gender_opt = 10; | ||
} | ||
|
||
|
||
message Company{ | ||
string name = 1; | ||
repeated employee employees = 2; | ||
optional Person persons = 3; | ||
} | ||
|
||
enum gender{ | ||
unknown = 0; | ||
male = 1; | ||
female = 2; | ||
} |
Oops, something went wrong.