From 63e074ed3a3ff470811f8368afa5b0f00306559e Mon Sep 17 00:00:00 2001 From: ksoichiro Date: Sat, 30 Aug 2014 19:01:23 +0900 Subject: [PATCH 1/3] #14 Added support for integer-array. --- main.go | 72 +++++++++++-------- parse.go | 5 ++ parse_test.go | 40 +++++++++-- print.go | 20 ++++++ testdata/res/values/arrays.xml | 7 ++ .../RdotmTestApp.xcodeproj/project.pbxproj | 6 +- .../RdotmTestApp/Base.lproj/Main.storyboard | 19 +++-- .../Resources/Src/res/values/arrays.xml | 7 ++ .../RdotmTestApp/ViewController.h | 1 + .../RdotmTestApp/ViewController.m | 7 ++ 10 files changed, 140 insertions(+), 44 deletions(-) create mode 100644 testdata/res/values/arrays.xml create mode 100644 testdata/xcode/RdotmTestApp/RdotmTestApp/Resources/Src/res/values/arrays.xml diff --git a/main.go b/main.go index 7f992ce..2fc7c0e 100644 --- a/main.go +++ b/main.go @@ -11,26 +11,28 @@ import ( // Command line options type Options struct { - ResDir string - OutDir string - Class string - Clean bool - Localize bool - PrefixStrings string - PrefixIntegers string - PrefixColors string - PrefixDrawables string - Types map[string]bool + ResDir string + OutDir string + Class string + Clean bool + Localize bool + PrefixStrings string + PrefixIntegers string + PrefixColors string + PrefixDrawables string + PrefixIntegerArrays string + Types map[string]bool } // Resource model structure type Resources struct { - Language string `xml:"-"` - Strings []String `xml:"string"` - Integers []Integer `xml:"integer"` - Colors []Color `xml:"color"` - Drawables []Drawable `xml:"-"` - Items []Item `xml:"item"` + Language string `xml:"-"` + Strings []String `xml:"string"` + Integers []Integer `xml:"integer"` + Colors []Color `xml:"color"` + Drawables []Drawable `xml:"-"` + Items []Item `xml:"item"` + IntegerArrays []IntegerArray `xml:"integer-array"` } type String struct { @@ -58,6 +60,11 @@ type Drawable struct { Name string } +type IntegerArray struct { + Name string `xml:"name,attr"` + Items []Item `xml:"item"` +} + func main() { // Get command line options var ( @@ -70,7 +77,8 @@ func main() { pi = flag.String("pi", "integer_", "Prefix for generated integer methods.") pc = flag.String("pc", "color_", "Prefix for generated color methods.") pd = flag.String("pd", "drawable_", "Prefix for generated drawable methods.") - types = flag.String("types", "string,integer,color,drawable", "Types of resources. Separate with commas.") + pia = flag.String("pia", "array_integer_", "Prefix for generated integer array methods.") + types = flag.String("types", "string,integer,color,drawable,integer-array", "Types of resources. Separate with commas.") ) flag.Parse() if *resDir == "" || *outDir == "" { @@ -80,10 +88,11 @@ func main() { } typesSet := make(map[string]bool) validTypesSet := map[string]bool{ - "string": true, - "integer": true, - "color": true, - "drawable": true, + "string": true, + "integer": true, + "color": true, + "drawable": true, + "integer-array": true, } for _, t := range strings.Split(*types, ",") { if !validTypesSet[t] { @@ -96,14 +105,15 @@ func main() { // Parse resource XML files and generate source code parse(&Options{ - ResDir: *resDir, - OutDir: *outDir, - Class: *class, - Clean: *clean, - Localize: *localize, - PrefixStrings: *ps, - PrefixIntegers: *pi, - PrefixColors: *pc, - PrefixDrawables: *pd, - Types: typesSet}) + ResDir: *resDir, + OutDir: *outDir, + Class: *class, + Clean: *clean, + Localize: *localize, + PrefixStrings: *ps, + PrefixIntegers: *pi, + PrefixColors: *pc, + PrefixDrawables: *pd, + PrefixIntegerArrays: *pia, + Types: typesSet}) } diff --git a/parse.go b/parse.go index 80b95cb..469ea27 100644 --- a/parse.go +++ b/parse.go @@ -105,6 +105,11 @@ func parseLang(opt *Options, valuesDir string) (res Resources) { } } } + if opt.Types["integer-array"] { + if 0 < len(r.IntegerArrays) { + res.IntegerArrays = append(res.IntegerArrays, r.IntegerArrays...) + } + } } return res } diff --git a/parse_test.go b/parse_test.go index d161c60..e24e7cf 100644 --- a/parse_test.go +++ b/parse_test.go @@ -7,10 +7,11 @@ import ( ) var allTypes = map[string]bool{ - "string": true, - "integer": true, - "color": true, - "drawable": true, + "string": true, + "integer": true, + "color": true, + "drawable": true, + "integer-array": true, } func TestParseDrawable(t *testing.T) { @@ -81,6 +82,21 @@ func TestParseLang(t *testing.T) { if res.Integers[1].Value != "20" { t.Errorf("Expected name '%s' but was '%s'\n", "20", res.Integers[1].Value) } + if len(res.IntegerArrays) != 1 { + t.Errorf("Expected %d integer arrays but was %d\n", 1, len(res.IntegerArrays)) + } + if len(res.IntegerArrays[0].Items) != 3 { + t.Errorf("Expected %d integer items but was %d\n", 3, len(res.IntegerArrays[0].Items)) + } + if res.IntegerArrays[0].Items[0].Value != "10" { + t.Errorf("Expected value '%s' but was '%s'\n", "10", res.IntegerArrays[0].Items[0].Value) + } + if res.IntegerArrays[0].Items[1].Value != "20" { + t.Errorf("Expected value '%s' but was '%s'\n", "20", res.IntegerArrays[0].Items[1].Value) + } + if res.IntegerArrays[0].Items[2].Value != "30" { + t.Errorf("Expected value '%s' but was '%s'\n", "30", res.IntegerArrays[0].Items[2].Value) + } res = parseXml("invalid") if len(res.Integers) != 0 { t.Errorf("Expected %d strings but was %d\n", 0, len(res.Integers)) @@ -89,9 +105,10 @@ func TestParseLang(t *testing.T) { func TestParseLangPartial(t *testing.T) { var allTypesTests = []map[string]bool{ - {"string": true, "integer": false, "color": false, "drawable": false}, - {"string": false, "integer": true, "color": false, "drawable": false}, - {"string": false, "integer": false, "color": true, "drawable": false}, + {"string": true, "integer": false, "color": false, "drawable": false, "integer-array": false}, + {"string": false, "integer": true, "color": false, "drawable": false, "integer-array": false}, + {"string": false, "integer": false, "color": true, "drawable": false, "integer-array": false}, + {"string": false, "integer": false, "color": false, "drawable": false, "integer-array": true}, } for _, tests := range allTypesTests { @@ -123,5 +140,14 @@ func TestParseLangPartial(t *testing.T) { t.Errorf("Expected no colors but was %d\n", len(res.Colors)) } } + if tests["integer-array"] { + if len(res.IntegerArrays) == 0 { + t.Errorf("Expected some integer arrays but was nothing\n") + } + } else { + if len(res.IntegerArrays) != 0 { + t.Errorf("Expected no integer arrays but was %d\n", len(res.IntegerArrays)) + } + } } } diff --git a/print.go b/print.go index 29b4388..57db238 100644 --- a/print.go +++ b/print.go @@ -84,6 +84,13 @@ func printAsObjectiveC(res *Resources, opt *Options) { `, opt.PrefixDrawables, d.Name)) } + // Integer array + for _, i := range res.IntegerArrays { + // Method definition + f.WriteString(fmt.Sprintf(`+ (NSArray *)%s%s; +`, opt.PrefixIntegerArrays, i.Name)) + } + f.WriteString(` @end `) @@ -132,6 +139,19 @@ func printAsObjectiveC(res *Resources, opt *Options) { f.WriteString(fmt.Sprintf("+ (UIImage *)%s%s { return [UIImage imageNamed:@\"%s\"]; }\n", opt.PrefixDrawables, d.Name, d.Name)) } + // Integer array + for _, i := range res.IntegerArrays { + var v = "" + for _, n := range i.Items { + if v != "" { + v += ", " + } + v += "@" + n.Value + } + // Method implementation + f.WriteString(fmt.Sprintf("+ (NSArray *)%s%s { return @[%s]; }\n", opt.PrefixIntegerArrays, i.Name, v)) + } + f.WriteString(` @end `) diff --git a/testdata/res/values/arrays.xml b/testdata/res/values/arrays.xml new file mode 100644 index 0000000..bd9feaa --- /dev/null +++ b/testdata/res/values/arrays.xml @@ -0,0 +1,7 @@ + + + 10 + 20 + 30 + + diff --git a/testdata/xcode/RdotmTestApp/RdotmTestApp.xcodeproj/project.pbxproj b/testdata/xcode/RdotmTestApp/RdotmTestApp.xcodeproj/project.pbxproj index 0767784..be8ad6a 100644 --- a/testdata/xcode/RdotmTestApp/RdotmTestApp.xcodeproj/project.pbxproj +++ b/testdata/xcode/RdotmTestApp/RdotmTestApp.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ DE3246B3198D4FF0003A83CA /* R.m in Sources */ = {isa = PBXBuildFile; fileRef = DE3246B2198D4FF0003A83CA /* R.m */; }; DE37BA4C19925CF400C22CD2 /* star.png in Resources */ = {isa = PBXBuildFile; fileRef = DE37BA4A19925CF400C22CD2 /* star.png */; }; DE37BA4D19925CF400C22CD2 /* star@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = DE37BA4B19925CF400C22CD2 /* star@2x.png */; }; + DE54717F19B1D0D60086F8A7 /* arrays.xml in Resources */ = {isa = PBXBuildFile; fileRef = DE54717E19B1D0D60086F8A7 /* arrays.xml */; }; DE5E0B7F199F1EB80030B30B /* integers.xml in Resources */ = {isa = PBXBuildFile; fileRef = DE5E0B7E199F1EB70030B30B /* integers.xml */; }; /* End PBXBuildFile section */ @@ -36,6 +37,7 @@ DE3246B2198D4FF0003A83CA /* R.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = R.m; sourceTree = ""; }; DE37BA4A19925CF400C22CD2 /* star.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = star.png; sourceTree = ""; }; DE37BA4B19925CF400C22CD2 /* star@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "star@2x.png"; sourceTree = ""; }; + DE54717E19B1D0D60086F8A7 /* arrays.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = arrays.xml; sourceTree = ""; }; DE5E0B7E199F1EB70030B30B /* integers.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = integers.xml; sourceTree = ""; }; /* End PBXFileReference section */ @@ -119,6 +121,7 @@ DE3246A0198D4BBB003A83CA /* values */ = { isa = PBXGroup; children = ( + DE54717E19B1D0D60086F8A7 /* arrays.xml */, DE5E0B7E199F1EB70030B30B /* integers.xml */, DE302170198E06E10039FE42 /* colors.xml */, DE3246A2198D4BBB003A83CA /* strings.xml */, @@ -205,6 +208,7 @@ DE37BA4D19925CF400C22CD2 /* star@2x.png in Resources */, DE3246A4198D4BBB003A83CA /* strings.xml in Resources */, DE37BA4C19925CF400C22CD2 /* star.png in Resources */, + DE54717F19B1D0D60086F8A7 /* arrays.xml in Resources */, DE324685198D4AB9003A83CA /* Main.storyboard in Resources */, DE324687198D4AB9003A83CA /* Images.xcassets in Resources */, DE302171198E06E10039FE42 /* colors.xml in Resources */, @@ -292,7 +296,7 @@ GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 7.0; MTL_ENABLE_DEBUG_INFO = YES; - ONLY_ACTIVE_ARCH = YES; + ONLY_ACTIVE_ARCH = NO; SDKROOT = iphoneos; }; name = Debug; diff --git a/testdata/xcode/RdotmTestApp/RdotmTestApp/Base.lproj/Main.storyboard b/testdata/xcode/RdotmTestApp/RdotmTestApp/Base.lproj/Main.storyboard index 4ee31f7..83a724d 100644 --- a/testdata/xcode/RdotmTestApp/RdotmTestApp/Base.lproj/Main.storyboard +++ b/testdata/xcode/RdotmTestApp/RdotmTestApp/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + @@ -39,8 +39,7 @@ diff --git a/testdata/xcode/RdotmTestApp/RdotmTestApp/Resources/Src/res/values/arrays.xml b/testdata/xcode/RdotmTestApp/RdotmTestApp/Resources/Src/res/values/arrays.xml new file mode 100644 index 0000000..bd9feaa --- /dev/null +++ b/testdata/xcode/RdotmTestApp/RdotmTestApp/Resources/Src/res/values/arrays.xml @@ -0,0 +1,7 @@ + + + 10 + 20 + 30 + + diff --git a/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.h b/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.h index b19121d..1595a23 100644 --- a/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.h +++ b/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.h @@ -11,6 +11,7 @@ @property (strong, nonatomic) IBOutlet UILabel *message; @property (strong, nonatomic) IBOutlet UILabel *message2; +@property (strong, nonatomic) IBOutlet UILabel *message3; @property (strong, nonatomic) IBOutlet UIImageView *image; @end diff --git a/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.m b/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.m index 9989b65..cd4653a 100644 --- a/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.m +++ b/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.m @@ -30,6 +30,13 @@ - (void)viewDidLoad { // Drawables [self.image setImage:[R drawable_star]]; + + // Integer array + NSMutableString *message3 = [NSMutableString stringWithString:@""]; + for (NSNumber *i in [R array_integer_foobar]) { + [message3 appendFormat:@"%@ ", i]; + } + [self.message3 setText:message3]; } @end From a6cd16086e7e0acc70a04b311415591439f4f5bd Mon Sep 17 00:00:00 2001 From: ksoichiro Date: Sat, 30 Aug 2014 19:13:09 +0900 Subject: [PATCH 2/3] #14 Added support for string-array. --- main.go | 12 ++++++++++- parse.go | 5 +++++ parse_test.go | 19 ++++++++++++++---- print.go | 20 +++++++++++++++++++ testdata/res/values/arrays.xml | 6 ++++++ .../Resources/Src/res/values/arrays.xml | 6 ++++++ .../RdotmTestApp/ViewController.m | 6 ++++++ 7 files changed, 69 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 2fc7c0e..9bfcf38 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ type Options struct { PrefixColors string PrefixDrawables string PrefixIntegerArrays string + PrefixStringArrays string Types map[string]bool } @@ -33,6 +34,7 @@ type Resources struct { Drawables []Drawable `xml:"-"` Items []Item `xml:"item"` IntegerArrays []IntegerArray `xml:"integer-array"` + StringArrays []StringArray `xml:"string-array"` } type String struct { @@ -65,6 +67,11 @@ type IntegerArray struct { Items []Item `xml:"item"` } +type StringArray struct { + Name string `xml:"name,attr"` + Items []Item `xml:"item"` +} + func main() { // Get command line options var ( @@ -78,7 +85,8 @@ func main() { pc = flag.String("pc", "color_", "Prefix for generated color methods.") pd = flag.String("pd", "drawable_", "Prefix for generated drawable methods.") pia = flag.String("pia", "array_integer_", "Prefix for generated integer array methods.") - types = flag.String("types", "string,integer,color,drawable,integer-array", "Types of resources. Separate with commas.") + psa = flag.String("psa", "array_string_", "Prefix for generated string array methods.") + types = flag.String("types", "string,integer,color,drawable,integer-array,string-array", "Types of resources. Separate with commas.") ) flag.Parse() if *resDir == "" || *outDir == "" { @@ -93,6 +101,7 @@ func main() { "color": true, "drawable": true, "integer-array": true, + "string-array": true, } for _, t := range strings.Split(*types, ",") { if !validTypesSet[t] { @@ -115,5 +124,6 @@ func main() { PrefixColors: *pc, PrefixDrawables: *pd, PrefixIntegerArrays: *pia, + PrefixStringArrays: *psa, Types: typesSet}) } diff --git a/parse.go b/parse.go index 469ea27..0077e52 100644 --- a/parse.go +++ b/parse.go @@ -110,6 +110,11 @@ func parseLang(opt *Options, valuesDir string) (res Resources) { res.IntegerArrays = append(res.IntegerArrays, r.IntegerArrays...) } } + if opt.Types["string-array"] { + if 0 < len(r.StringArrays) { + res.StringArrays = append(res.StringArrays, r.StringArrays...) + } + } } return res } diff --git a/parse_test.go b/parse_test.go index e24e7cf..3bf45c0 100644 --- a/parse_test.go +++ b/parse_test.go @@ -12,6 +12,7 @@ var allTypes = map[string]bool{ "color": true, "drawable": true, "integer-array": true, + "string-array": true, } func TestParseDrawable(t *testing.T) { @@ -105,10 +106,11 @@ func TestParseLang(t *testing.T) { func TestParseLangPartial(t *testing.T) { var allTypesTests = []map[string]bool{ - {"string": true, "integer": false, "color": false, "drawable": false, "integer-array": false}, - {"string": false, "integer": true, "color": false, "drawable": false, "integer-array": false}, - {"string": false, "integer": false, "color": true, "drawable": false, "integer-array": false}, - {"string": false, "integer": false, "color": false, "drawable": false, "integer-array": true}, + {"string": true, "integer": false, "color": false, "drawable": false, "integer-array": false, "string-array": false}, + {"string": false, "integer": true, "color": false, "drawable": false, "integer-array": false, "string-array": false}, + {"string": false, "integer": false, "color": true, "drawable": false, "integer-array": false, "string-array": false}, + {"string": false, "integer": false, "color": false, "drawable": false, "integer-array": true, "string-array": false}, + {"string": false, "integer": false, "color": false, "drawable": false, "integer-array": false, "string-array": true}, } for _, tests := range allTypesTests { @@ -149,5 +151,14 @@ func TestParseLangPartial(t *testing.T) { t.Errorf("Expected no integer arrays but was %d\n", len(res.IntegerArrays)) } } + if tests["string-array"] { + if len(res.StringArrays) == 0 { + t.Errorf("Expected some string arrays but was nothing\n") + } + } else { + if len(res.StringArrays) != 0 { + t.Errorf("Expected no string arrays but was %d\n", len(res.StringArrays)) + } + } } } diff --git a/print.go b/print.go index 57db238..75f6602 100644 --- a/print.go +++ b/print.go @@ -91,6 +91,13 @@ func printAsObjectiveC(res *Resources, opt *Options) { `, opt.PrefixIntegerArrays, i.Name)) } + // String array + for _, i := range res.StringArrays { + // Method definition + f.WriteString(fmt.Sprintf(`+ (NSArray *)%s%s; +`, opt.PrefixStringArrays, i.Name)) + } + f.WriteString(` @end `) @@ -152,6 +159,19 @@ func printAsObjectiveC(res *Resources, opt *Options) { f.WriteString(fmt.Sprintf("+ (NSArray *)%s%s { return @[%s]; }\n", opt.PrefixIntegerArrays, i.Name, v)) } + // String array + for _, i := range res.StringArrays { + var v = "" + for _, n := range i.Items { + if v != "" { + v += ", " + } + v += "@\"" + n.Value + "\"" + } + // Method implementation + f.WriteString(fmt.Sprintf("+ (NSArray *)%s%s { return @[%s]; }\n", opt.PrefixStringArrays, i.Name, v)) + } + f.WriteString(` @end `) diff --git a/testdata/res/values/arrays.xml b/testdata/res/values/arrays.xml index bd9feaa..8d0f73d 100644 --- a/testdata/res/values/arrays.xml +++ b/testdata/res/values/arrays.xml @@ -4,4 +4,10 @@ 20 30 + + + hoge + fuga + piyo + diff --git a/testdata/xcode/RdotmTestApp/RdotmTestApp/Resources/Src/res/values/arrays.xml b/testdata/xcode/RdotmTestApp/RdotmTestApp/Resources/Src/res/values/arrays.xml index bd9feaa..8d0f73d 100644 --- a/testdata/xcode/RdotmTestApp/RdotmTestApp/Resources/Src/res/values/arrays.xml +++ b/testdata/xcode/RdotmTestApp/RdotmTestApp/Resources/Src/res/values/arrays.xml @@ -4,4 +4,10 @@ 20 30 + + + hoge + fuga + piyo + diff --git a/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.m b/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.m index cd4653a..ce99e49 100644 --- a/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.m +++ b/testdata/xcode/RdotmTestApp/RdotmTestApp/ViewController.m @@ -36,6 +36,12 @@ - (void)viewDidLoad { for (NSNumber *i in [R array_integer_foobar]) { [message3 appendFormat:@"%@ ", i]; } + + // String array + for (NSString *s in [R array_string_blurblur]) { + [message3 appendFormat:@"%@ ", s]; + } + [self.message3 setText:message3]; } From 26a15a3b3d217310323d677796b1d15ac8ea38f2 Mon Sep 17 00:00:00 2001 From: ksoichiro Date: Sat, 30 Aug 2014 19:18:58 +0900 Subject: [PATCH 3/3] Updated README. Added `integer-array` and `string-array`. --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0461400..64853b3 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,9 @@ res | `-pi` | integer_ | Prefix for generated integer methods. | | `-pc` | color_ | Prefix for generated color methods. | | `-pd` | drawable_ | Prefix for generated drawable methods. | -| `-types` | string,integer,color,drawable | Types of resources. Separate with commas. | +| `-pia` | array_integer_ | Prefix for generated integer array methods. | +| `-psa` | array_string_ | Prefix for generated string array methods. | +| `-types` | string,integer,color,drawable,integer-array,string-array | Types of resources. Separate with commas. | ## Example @@ -198,6 +200,24 @@ res/values/colors.xml ``` +res/values/arrays.xml + +```xml + + + 10 + 20 + 30 + + + + hoge + fuga + piyo + + +``` + res/drawables ``` @@ -230,6 +250,8 @@ R.h /** #990099cc */ + (UIColor *)color_default_text; + (UIImage *)drawable_star; ++ (NSArray *)array_integer_foobar; ++ (NSArray *)array_string_blurblur; @end ``` @@ -251,6 +273,8 @@ R.m + (UIColor *)color_default_bg { return [UIColor colorWithRed:187/255.0 green:238/255.0 blue:255/255.0 alpha:255/255.0]; } + (UIColor *)color_default_text { return [UIColor colorWithRed:0/255.0 green:153/255.0 blue:204/255.0 alpha:153/255.0]; } + (UIImage *)drawable_star { return [UIImage imageNamed:@"star"]; } ++ (NSArray *)array_integer_foobar { return @[@10, @20, @30]; } ++ (NSArray *)array_string_blurblur { return @[@"hoge", @"fuga", @"piyo"]; } @end ```