|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/go-sphere/sphere-cli/internal/entproto" |
| 4 | + "github.com/go-sphere/sphere-cli/internal/entity" |
| 5 | + "github.com/go-sphere/sphere-cli/internal/entity/graph" |
5 | 6 | "github.com/spf13/cobra" |
| 7 | + "github.com/spf13/pflag" |
6 | 8 | ) |
7 | 9 |
|
| 10 | +type EntSharedOptions struct { |
| 11 | + SchemaPath string |
| 12 | + |
| 13 | + AllFieldsRequired bool |
| 14 | + AutoAddAnnotation bool |
| 15 | + EnumUseRawType bool |
| 16 | + SkipUnsupported bool |
| 17 | + |
| 18 | + TimeProtoType string |
| 19 | + UUIDProtoType string |
| 20 | + UnsupportedProtoType string |
| 21 | + |
| 22 | + ProtoPackages string |
| 23 | +} |
| 24 | + |
| 25 | +func (o *EntSharedOptions) AddFlags(fs *pflag.FlagSet) { |
| 26 | + fs.StringVar(&o.SchemaPath, "path", "./schema", "path to schema directory") |
| 27 | + |
| 28 | + fs.StringVar(&o.TimeProtoType, "time_proto_type", "int64", "use proto type for time.Time, one of int64, string, google.protobuf.Timestamp") |
| 29 | + fs.StringVar(&o.UUIDProtoType, "uuid_proto_type", "string", "use proto type for uuid.UUID, one of string, bytes") |
| 30 | + fs.StringVar(&o.UnsupportedProtoType, "unsupported_proto_type", "google.protobuf.Any", "use proto type for unsupported types, one of google.protobuf.Any, google.protobuf.Struct, bytes") |
| 31 | + |
| 32 | + fs.BoolVar(&o.AllFieldsRequired, "all_fields_required", true, "ignore optional, use zero value instead") |
| 33 | + fs.BoolVar(&o.AutoAddAnnotation, "auto_annotation", true, "auto add annotation to the schema") |
| 34 | + fs.BoolVar(&o.EnumUseRawType, "enum_raw_type", true, "use string for enum") |
| 35 | + fs.BoolVar(&o.SkipUnsupported, "skip_unsupported", true, "skip unsupported types, when unsupportedProtoType is not set") |
| 36 | + |
| 37 | + fs.StringVar(&o.ProtoPackages, "import_proto", "google/protobuf/any.proto,google.protobuf,Any;", "import proto, format: path1,package1,type1,type2;path2,package2,type3,type4;") |
| 38 | +} |
| 39 | + |
| 40 | +func (o *EntSharedOptions) ToGraphOptions() *graph.Options { |
| 41 | + return &graph.Options{ |
| 42 | + SchemaPath: o.SchemaPath, |
| 43 | + AllFieldsRequired: o.AllFieldsRequired, |
| 44 | + AutoAddAnnotation: o.AutoAddAnnotation, |
| 45 | + EnumUseRawType: o.EnumUseRawType, |
| 46 | + SkipUnsupported: o.SkipUnsupported, |
| 47 | + TimeProtoType: o.TimeProtoType, |
| 48 | + UUIDProtoType: o.UUIDProtoType, |
| 49 | + UnsupportedProtoType: o.UnsupportedProtoType, |
| 50 | + } |
| 51 | +} |
| 52 | + |
8 | 53 | var ent2protoCmd = &cobra.Command{ |
9 | 54 | Use: "entproto", |
10 | 55 | Aliases: []string{"ent2proto"}, |
11 | 56 | Short: "Convert Ent schema to Protobuf definitions", |
12 | 57 | Long: `Convert Ent schema to Protobuf definitions, generating .proto files from Ent schema definitions.`, |
13 | 58 | } |
14 | 59 |
|
| 60 | +var ent2mapperCmd = &cobra.Command{ |
| 61 | + Use: "entmapper", |
| 62 | + Aliases: []string{"ent2mapper"}, |
| 63 | + Short: "Convert Ent schema to Ent mapper", |
| 64 | + Long: `Convert Ent schema to Ent mapper, generating mapper files from Ent schema definitions.`, |
| 65 | +} |
| 66 | + |
15 | 67 | func init() { |
| 68 | + shareOptions := &EntSharedOptions{} |
| 69 | + shareOptions.AddFlags(ent2protoCmd.Flags()) |
| 70 | + shareOptions.AddFlags(ent2mapperCmd.Flags()) |
16 | 71 | rootCmd.AddCommand(ent2protoCmd) |
| 72 | + rootCmd.AddCommand(ent2mapperCmd) |
17 | 73 |
|
18 | | - flag := ent2protoCmd.Flags() |
19 | | - schemaPath := flag.String("path", "./schema", "path to schema directory") |
20 | | - protoDir := flag.String("proto", "./proto", "path to proto directory") |
21 | | - |
22 | | - timeProtoType := flag.String("time_proto_type", "int64", "use proto type for time.Time, one of int64, string, google.protobuf.Timestamp") |
23 | | - uuidProtoType := flag.String("uuid_proto_type", "string", "use proto type for uuid.UUID, one of string, bytes") |
24 | | - unsupportedProtoType := flag.String("unsupported_proto_type", "google.protobuf.Any", "use proto type for unsupported types, one of google.protobuf.Any, google.protobuf.Struct, bytes") |
25 | | - |
26 | | - allFieldsRequired := flag.Bool("all_fields_required", true, "ignore optional, use zero value instead") |
27 | | - autoAddAnnotation := flag.Bool("auto_annotation", true, "auto add annotation to the schema") |
28 | | - enumUseRawType := flag.Bool("enum_raw_type", true, "use string for enum") |
29 | | - skipUnsupported := flag.Bool("skip_unsupported", true, "skip unsupported types, when unsupportedProtoType is not set") |
30 | | - |
31 | | - importProto := flag.String("import_proto", "google/protobuf/any.proto,google.protobuf,Any;", "import proto, format: path1,package1,type1,type2;path2,package2,type3,type4;") |
32 | | - |
33 | | - ent2protoCmd.RunE = func(cmd *cobra.Command, args []string) error { |
34 | | - options := entproto.Options{ |
35 | | - SchemaPath: *schemaPath, |
36 | | - ProtoDir: *protoDir, |
37 | | - |
38 | | - TimeProtoType: *timeProtoType, |
39 | | - UUIDProtoType: *uuidProtoType, |
40 | | - UnsupportedProtoType: *unsupportedProtoType, |
41 | | - SkipUnsupported: *skipUnsupported, |
42 | | - |
43 | | - AllFieldsRequired: *allFieldsRequired, |
44 | | - AutoAddAnnotation: *autoAddAnnotation, |
45 | | - EnumUseRawType: *enumUseRawType, |
| 74 | + { |
| 75 | + flag := ent2protoCmd.Flags() |
| 76 | + protoDir := flag.String("proto", "./proto", "path to proto directory") |
| 77 | + ent2protoCmd.RunE = func(cmd *cobra.Command, args []string) error { |
| 78 | + return entity.GenerateProto(&entity.ProtoOptions{ |
| 79 | + Graph: shareOptions.ToGraphOptions(), |
| 80 | + ProtoDir: *protoDir, |
| 81 | + }) |
| 82 | + } |
46 | 83 |
|
47 | | - ProtoPackages: entproto.ParseProtoPackages(*importProto), |
| 84 | + } |
| 85 | + { |
| 86 | + flag := ent2mapperCmd.Flags() |
| 87 | + mapperDir := flag.String("mapper", "./mapper", "path to mapper directory") |
| 88 | + mapperPackage := flag.String("mapper_package", "mapper", "package name for the generated mapper code") |
| 89 | + entPackage := flag.String("ent_package", "ent", "package name for the ent code") |
| 90 | + protoPkgPath := flag.String("proto_pkg_path", "github.com/go-sphere/sphere-layout/proto", "go module path for the generated proto code") |
| 91 | + protoPkgName := flag.String("proto_pkg_name", "proto", "package name for the generated proto code") |
| 92 | + ent2mapperCmd.RunE = func(cmd *cobra.Command, args []string) error { |
| 93 | + return entity.GenerateMapper(&entity.MapperOptions{ |
| 94 | + Graph: shareOptions.ToGraphOptions(), |
| 95 | + MapperDir: *mapperDir, |
| 96 | + MapperPackage: *mapperPackage, |
| 97 | + EntPackage: *entPackage, |
| 98 | + ProtoPkgPath: *protoPkgPath, |
| 99 | + ProtoPkgName: *protoPkgName, |
| 100 | + }) |
48 | 101 | } |
49 | | - entproto.Generate(&options) |
50 | | - return nil |
51 | 102 | } |
52 | 103 | } |
0 commit comments