Skip to content

Commit

Permalink
add format option to file import
Browse files Browse the repository at this point in the history
  • Loading branch information
weynsee committed Mar 8, 2015
1 parent f31bba7 commit a063f40
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
14 changes: 14 additions & 0 deletions phrase/file_imports.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package phrase

import (
"fmt"
"github.com/google/go-querystring/query"
"io"
"net/url"
)

// FileImportsService provides access to the file upload service
Expand Down Expand Up @@ -33,8 +35,13 @@ type FileImportRequest struct {

// Enable Emoji conversion.
ConvertEmoji bool `url:"file_import[convert_emoji],int,omitempty"`

// Some formats can have additional options. Please check the format guide for more information about available options.
FormatOptions formatOptions `url:"file_import[format_options],omitempty"`
}

type formatOptions map[string]interface{}

// Upload a localization file.
//
// Phrase API docs: http://docs.phraseapp.com/api/v1/file_imports/
Expand All @@ -54,3 +61,10 @@ func (s *FileImportsService) Upload(i *FileImportRequest, reader io.Reader) erro
_, err = s.client.Do(req, resp)
return err
}

func (o formatOptions) EncodeValues(key string, v *url.Values) error {
for k, val := range o {
(*v)[key+fmt.Sprintf("[%s]", k)] = []string{fmt.Sprintf("%v", val)}
}
return nil
}
34 changes: 24 additions & 10 deletions phrase/file_imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestFileImportsService_Upload(t *testing.T) {
setup()
defer teardown()
filename := "en.yml"
filename := "en.csv"

mux.HandleFunc("/file_imports", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(100000)
Expand All @@ -21,14 +21,20 @@ func TestFileImportsService_Upload(t *testing.T) {
}
testMethod(t, r, "POST")
testParams(t, r.MultipartForm.Value, map[string]string{
"file_import[skip_upload_tags]": "1",
"file_import[skip_unverification]": "1",
"file_import[convert_emoji]": "1",
"file_import[locale_code]": "en",
"file_import[filename]": filename,
"file_import[format]": "yml",
"file_import[tag_names]": "new,upload",
"file_import[update_translations]": "1",
"file_import[skip_upload_tags]": "1",
"file_import[skip_unverification]": "1",
"file_import[convert_emoji]": "1",
"file_import[locale_code]": "en",
"file_import[filename]": filename,
"file_import[format]": "csv",
"file_import[tag_names]": "new,upload",
"file_import[update_translations]": "1",
"file_import[format_options][key_index]": "1",
"file_import[format_options][translation_index]": "2",
"file_import[format_options][comment_index]": "3",
"file_import[format_options][column_separator]": ",",
"file_import[format_options][quote_char]": "\"",
"file_import[format_options][header_content_row]": "false",
})
file := r.MultipartForm.File["file_import[file]"][0]
if file.Filename != filename {
Expand All @@ -50,12 +56,20 @@ func TestFileImportsService_Upload(t *testing.T) {
upload := &FileImportRequest{
Locale: "en",
Filename: filename,
Format: "yml",
Format: "csv",
Tags: []string{"new", "upload"},
UpdateTranslations: true,
SkipUnverification: true,
SkipUploadTags: true,
ConvertEmoji: true,
FormatOptions: map[string]interface{}{
"key_index": 1,
"translation_index": 2,
"comment_index": 3,
"column_separator": ",",
"quote_char": "\"",
"header_content_row": false,
},
}
err := client.FileImports.Upload(upload, strings.NewReader("this is a test"))
if err != nil {
Expand Down

0 comments on commit a063f40

Please sign in to comment.