Skip to content

Commit

Permalink
Merge pull request #15 from s0nerik/feature/generic_source_support
Browse files Browse the repository at this point in the history
Feature/generic source support
  • Loading branch information
s0nerik authored Jul 28, 2019
2 parents d05e3a1 + a8c514f commit bb1ecf2
Show file tree
Hide file tree
Showing 16 changed files with 322 additions and 221 deletions.
48 changes: 20 additions & 28 deletions goloc/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// ParseFormats parses formats given the raw table data and returns, if successful, mappings
// to the actual platform format for each format name.
func ParseFormats(
rawData [][]interface{},
rawData [][]RawCell,
platform Platform,
formatsTabName string,
formatColumnTitle string,
Expand All @@ -33,35 +33,27 @@ func ParseFormats(
platformName: actualPlatformName,
}
}
if key, ok := row[formatColIndex].(FormatKey); ok {
if val, ok := row[platformColIndex].(string); ok {
trimmedVal := strings.TrimSpace(val)
if len(trimmedVal) == 0 {
return nil, &formatValueNotSpecifiedError{
cell: *NewCell(formatsTabName, actualRowIndex, uint(platformColIndex)),
platformName: actualPlatformName,
}
}
err := platform.ValidateFormat(trimmedVal)
if err != nil {
return nil, &formatValueInvalidError{
cell: *NewCell(formatsTabName, actualRowIndex, uint(platformColIndex)),
platformName: actualPlatformName,
formatValue: trimmedVal,
reason: err,
}
}
formats[key] = trimmedVal
} else {
return nil, &wrongValueTypeError{
cell: *NewCell(formatsTabName, actualRowIndex, uint(platformColIndex)),
}

key := row[formatColIndex]
val := row[platformColIndex]

trimmedVal := strings.TrimSpace(val)
if len(trimmedVal) == 0 {
return nil, &formatValueNotSpecifiedError{
cell: *NewCell(formatsTabName, actualRowIndex, uint(platformColIndex)),
platformName: actualPlatformName,
}
} else {
return nil, &wrongKeyTypeError{
cell: *NewCell(formatsTabName, actualRowIndex, uint(formatColIndex)),
}
err := platform.ValidateFormat(trimmedVal)
if err != nil {
return nil, &formatValueInvalidError{
cell: *NewCell(formatsTabName, actualRowIndex, uint(platformColIndex)),
platformName: actualPlatformName,
formatValue: trimmedVal,
reason: err,
}
}
formats[key] = trimmedVal
}

// Handle default format ("{}")
Expand All @@ -74,7 +66,7 @@ func ParseFormats(

func columnIndices(
platform Platform,
rawData [][]interface{},
rawData [][]RawCell,
formatsTabName string,
formatColumnTitle string,
) (formatColIndex int, platformColIndex int, actualPlatformName string, err error) {
Expand Down
60 changes: 10 additions & 50 deletions goloc/formats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestFormatsEmptyData(t *testing.T) {
var data [][]interface{}
var data [][]RawCell

platform := newMockPlatform(nil)
_, err := ParseFormats(data, platform, "", "", "{}")
Expand All @@ -17,7 +17,7 @@ func TestFormatsEmptyData(t *testing.T) {
}

func TestFormatsEmptyFirstRow(t *testing.T) {
data := [][]interface{}{
data := [][]RawCell{
{},
{"x"},
}
Expand All @@ -30,7 +30,7 @@ func TestFormatsEmptyFirstRow(t *testing.T) {
}

func TestFormatsMissingFormatColumn(t *testing.T) {
data := [][]interface{}{
data := [][]RawCell{
{"mock"},
}

Expand All @@ -42,7 +42,7 @@ func TestFormatsMissingFormatColumn(t *testing.T) {
}

func TestFormatsMissingPlatformColumn(t *testing.T) {
data := [][]interface{}{
data := [][]RawCell{
{"format"},
}

Expand All @@ -54,7 +54,7 @@ func TestFormatsMissingPlatformColumn(t *testing.T) {
}

func TestFormatsMissingFormatKey(t *testing.T) {
data := [][]interface{}{
data := [][]RawCell{
{"mock", "format"},
{""},
}
Expand All @@ -67,7 +67,7 @@ func TestFormatsMissingFormatKey(t *testing.T) {
}

func TestFormatsMissingFormatValue1(t *testing.T) {
data := [][]interface{}{
data := [][]RawCell{
{"format", "mock"},
{""},
}
Expand All @@ -80,7 +80,7 @@ func TestFormatsMissingFormatValue1(t *testing.T) {
}

func TestFormatsMissingFormatValue2(t *testing.T) {
data := [][]interface{}{
data := [][]RawCell{
{"format", "mock"},
{"x", ""},
}
Expand All @@ -93,7 +93,7 @@ func TestFormatsMissingFormatValue2(t *testing.T) {
}

func TestFormatsMissingFormatValue3(t *testing.T) {
data := [][]interface{}{
data := [][]RawCell{
{"format", "mock"},
{"x", " "},
}
Expand All @@ -106,7 +106,7 @@ func TestFormatsMissingFormatValue3(t *testing.T) {
}

func TestFormatsFormatValidation(t *testing.T) {
data := [][]interface{}{
data := [][]RawCell{
{"format", "mock"},
{"x", "s"},
{"y", "%s"},
Expand All @@ -126,44 +126,4 @@ func TestFormatsFormatValidation(t *testing.T) {
platform.AssertCalled(t, "ValidateFormat", "s")
platform.AssertCalled(t, "ValidateFormat", "%s")
platform.AssertNumberOfCalls(t, "ValidateFormat", 2)
}

func TestFormatsWrongValueType(t *testing.T) {
data := [][]interface{}{
{"format", "mock"},
{"x", "s"},
{"y", 1},
{"z", "%s"},
}

platform := newMockPlatform(nil)

_, err := ParseFormats(data, platform, "", "format", "{}")
if assert.Error(t, err) {
assert.IsType(t, &wrongValueTypeError{}, err)
}

platform.AssertCalled(t, "ValidateFormat", "s")
platform.AssertNotCalled(t, "ValidateFormat", "%s")
platform.AssertNumberOfCalls(t, "ValidateFormat", 1)
}

func TestFormatsWrongKeyType(t *testing.T) {
data := [][]interface{}{
{"format", "mock"},
{"x", "s"},
{1, "%s"},
{"z", "%s"},
}

platform := newMockPlatform(nil)

_, err := ParseFormats(data, platform, "", "format", "{}")
if assert.Error(t, err) {
assert.IsType(t, &wrongKeyTypeError{}, err)
}

platform.AssertCalled(t, "ValidateFormat", "s")
platform.AssertNotCalled(t, "ValidateFormat", "%s")
platform.AssertNumberOfCalls(t, "ValidateFormat", 1)
}
}
66 changes: 13 additions & 53 deletions goloc/goloc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package goloc

import (
"io/ioutil"
"fmt"
"log"
"os"
"regexp"
Expand All @@ -11,11 +11,10 @@ import (
"sync"

"github.com/olekukonko/tablewriter"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/sheets/v4"
)

type RawCell = string

// Lang represents a language code.
type Lang = string

Expand All @@ -37,76 +36,39 @@ type Formats = map[FormatKey]string
// ResDir represents a resources directory path.
type ResDir = string

func sheetsAPI(credFilePath string) *sheets.SpreadsheetsService {
ctx := context.Background()

sec, err := ioutil.ReadFile(credFilePath)
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}

config, err := google.JWTConfigFromJSON(sec, "https://www.googleapis.com/auth/spreadsheets.readonly")
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}

s, err := sheets.New(config.Client(ctx))
if err != nil {
log.Fatalf("Unable to retrieve Sheets Client %v", err)
}

return s.Spreadsheets
}

func fetchRawValues(api *sheets.SpreadsheetsService, sheetID string, tab string) ([][]interface{}, error) {
resp, err := api.Values.Get(sheetID, tab).Do()
if err != nil {
return nil, err
}
return resp.Values, nil
}

func fetchEverythingRaw(
api *sheets.SpreadsheetsService,
sheetID string,
formatsTab string,
localizationsTab string,
) (rawFormats, rawLocalizations [][]interface{}, err error) {
func fetchEverythingRaw(source Source) (rawFormats, rawLocalizations [][]string, err error) {
var formatsError error
var localizationsError error

var wg sync.WaitGroup
go func() {
defer wg.Done()
rawFormats, formatsError = fetchRawValues(api, sheetID, formatsTab)
rawFormats, formatsError = source.Formats()
}()
go func() {
defer wg.Done()
rawLocalizations, localizationsError = fetchRawValues(api, sheetID, localizationsTab)
rawLocalizations, localizationsError = source.Localizations()
}()

wg.Add(2)
wg.Wait()

if formatsError != nil {
return nil, nil, formatsError
return nil, nil, fmt.Errorf(`can't load formats (%v)`, formatsError)
}
if localizationsError != nil {
return nil, nil, localizationsError
return nil, nil, fmt.Errorf(`can't load localizations (%v)`, formatsError)
}

return
}

// Run launches the actual process of fetching, parsing and writing the localization files.
func Run(
source Source,
platform Platform,
resDir string,
credFilePath string,
sheetID string,
tabName string,
keyColumn string,
formatsTabName string,
formatNameColumn string,
defaultLocalization string,
defaultLocalizationPath string,
Expand All @@ -115,19 +77,17 @@ func Run(
defFormatName string,
emptyLocalizationMatch *regexp.Regexp,
) {
api := sheetsAPI(credFilePath)

rawFormats, rawLocalizations, err := fetchEverythingRaw(api, sheetID, formatsTabName, tabName)
rawFormats, rawLocalizations, err := fetchEverythingRaw(source)
if err != nil {
log.Fatalf(`Can't fetch data from "%v" sheet. Reason: %v.`, sheetID, err)
log.Fatalf(`Can't fetch data. Reason: %v.`, err)
}

formats, err := ParseFormats(rawFormats, platform, formatsTabName, formatNameColumn, defFormatName)
formats, err := ParseFormats(rawFormats, platform, source.FormatsDocumentName(), formatNameColumn, defFormatName)
if err != nil {
log.Fatal(err)
}

localizations, fArgs, warn, err := ParseLocalizations(rawLocalizations, platform, formats, tabName, keyColumn, stopOnMissing, emptyLocalizationMatch)
localizations, fArgs, warn, err := ParseLocalizations(rawLocalizations, platform, formats, source.LocalizationsDocumentName(), keyColumn, stopOnMissing, emptyLocalizationMatch)
if err != nil {
log.Fatal(err)
} else {
Expand Down
Loading

0 comments on commit bb1ecf2

Please sign in to comment.