Skip to content

Plist and inline-swift template not working with Arrays #662

@fjtrujy

Description

@fjtrujy

Hello,
I'm trying to use the template plists/inline-swift4.stencil template to generate the content of a plist.

So far the content of the plist is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Names</key>
	<array>
		<string>Jhon</string>
		<string>Peter</string>
		<string>Nick</string>
	</array>
	<key>Surnames</key>
	<array>
		<string>Smith</string>
		<string>Jhonson</string>
		<string>Williams</string>
	</array>
</dict>
</plist>

And visually it looks:

Screenshot 2019-10-30 at 12 53 11

Then in the .swiftgen.yml I just have somethings as:

.........
plist:
    inputs: Precompile/OriginPlists/
    outputs:
        templateName: inline-swift4
        output: Generated/Plist.swift
.....

When I execute it, it generates wrong data:

internal static let names: [String] = [Jhon, Peter, Nick]
internal static let surnames: [String] = [Smith, Jhonson, Williams]

If you see it misses the quotes in every single element in the array, and in the plist, they are declared as String

Digging into the template I found where the issue "could be", I have created a solution, I tried locally and is working fine, but I don't know if this is breaking something else. Besides I see that the same logic is applied in several templates, so not sure if in these templates is broken as well.

The solution that I applied in the plists/inline-swift4.stencil was:

@@ -55,9 +55,9 @@ import Foundation
     Date(timeIntervalSinceReferenceDate: {{ value.timeIntervalSinceReferenceDate }})
   {% elif metadata.type == "Optional" %}
     nil
-  {% elif metadata.type == "Array" and metadata.element.items %}
-    [{% for itemMetadata in metadata.element.items %}
-      {% call valueBlock value[forloop.counter0] itemMetadata %}
+  {% elif metadata.type == "Array" and value %}
+    [{% for currentValue in value %}
+      {% call valueBlock currentValue metadata.element %}
       {% if not forloop.last %}, {% endif %}
     {% endfor %}]
   {% elif metadata.type == "Dictionary" %}

The new output is:

internal static let names: [String] = ["Jhon", "Peter", "Nick"]
internal static let surnames: [String] = ["Smith", "Jhonson", "Williams"]

🎉 🎉 🎉 🎉 🎉 🎉 🎉

I didn't create the PR, because I'm not sure if this will break something else or if I need to apply the same logic in more files.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions