Skip to content

Commit

Permalink
Closes #14: Merge branch '14_array_support'
Browse files Browse the repository at this point in the history
  • Loading branch information
ksoichiro committed Aug 30, 2014
2 parents ef067c6 + 26a15a3 commit e0908cf
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 45 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -198,6 +200,24 @@ res/values/colors.xml
</resources>
```

res/values/arrays.xml

```xml
<resources>
<integer-array name="foobar">
<item>10</item>
<item>20</item>
<item>30</item>
</integer-array>

<string-array name="blurblur">
<item>hoge</item>
<item>fuga</item>
<item>piyo</item>
</string-array>
</resources>
```

res/drawables

```
Expand Down Expand Up @@ -230,6 +250,8 @@ R.h
/** #990099cc */
+ (UIColor *)color_default_text;
+ (UIImage *)drawable_star;
+ (NSArray *)array_integer_foobar;
+ (NSArray *)array_string_blurblur;

@end
```
Expand All @@ -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
```
Expand Down
82 changes: 51 additions & 31 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,30 @@ 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
PrefixStringArrays 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"`
StringArrays []StringArray `xml:"string-array"`
}

type String struct {
Expand Down Expand Up @@ -58,6 +62,16 @@ type Drawable struct {
Name string
}

type IntegerArray struct {
Name string `xml:"name,attr"`
Items []Item `xml:"item"`
}

type StringArray struct {
Name string `xml:"name,attr"`
Items []Item `xml:"item"`
}

func main() {
// Get command line options
var (
Expand All @@ -70,7 +84,9 @@ 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.")
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 == "" {
Expand All @@ -80,10 +96,12 @@ 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,
"string-array": true,
}
for _, t := range strings.Split(*types, ",") {
if !validTypesSet[t] {
Expand All @@ -96,14 +114,16 @@ 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,
PrefixStringArrays: *psa,
Types: typesSet})
}
10 changes: 10 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ 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...)
}
}
if opt.Types["string-array"] {
if 0 < len(r.StringArrays) {
res.StringArrays = append(res.StringArrays, r.StringArrays...)
}
}
}
return res
}
Expand Down
51 changes: 44 additions & 7 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ 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,
"string-array": true,
}

func TestParseDrawable(t *testing.T) {
Expand Down Expand Up @@ -81,6 +83,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))
Expand All @@ -89,9 +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},
{"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-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 {
Expand Down Expand Up @@ -123,5 +142,23 @@ 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))
}
}
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))
}
}
}
}
40 changes: 40 additions & 0 deletions print.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ 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))
}

// String array
for _, i := range res.StringArrays {
// Method definition
f.WriteString(fmt.Sprintf(`+ (NSArray *)%s%s;
`, opt.PrefixStringArrays, i.Name))
}

f.WriteString(`
@end
`)
Expand Down Expand Up @@ -132,6 +146,32 @@ 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))
}

// 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
`)
Expand Down
13 changes: 13 additions & 0 deletions testdata/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<resources>
<integer-array name="foobar">
<item>10</item>
<item>20</item>
<item>30</item>
</integer-array>

<string-array name="blurblur">
<item>hoge</item>
<item>fuga</item>
<item>piyo</item>
</string-array>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -36,6 +37,7 @@
DE3246B2198D4FF0003A83CA /* R.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = R.m; sourceTree = "<group>"; };
DE37BA4A19925CF400C22CD2 /* star.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = star.png; sourceTree = "<group>"; };
DE37BA4B19925CF400C22CD2 /* star@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "star@2x.png"; sourceTree = "<group>"; };
DE54717E19B1D0D60086F8A7 /* arrays.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = arrays.xml; sourceTree = "<group>"; };
DE5E0B7E199F1EB70030B30B /* integers.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = integers.xml; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -119,6 +121,7 @@
DE3246A0198D4BBB003A83CA /* values */ = {
isa = PBXGroup;
children = (
DE54717E19B1D0D60086F8A7 /* arrays.xml */,
DE5E0B7E199F1EB70030B30B /* integers.xml */,
DE302170198E06E10039FE42 /* colors.xml */,
DE3246A2198D4BBB003A83CA /* strings.xml */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e0908cf

Please sign in to comment.