Skip to content

Commit

Permalink
swagger + languages/phone generator fixes (#24)
Browse files Browse the repository at this point in the history
* 6106 phone country code, phone number, language name 2 and 3 char codes and postal code types

* makefile gen-phone gen-language commands added

* swagger update

* fix travis-ci

* readme update

* swagger + languages/phone generator fixes

* swagger + languages/phone generator fixes

Co-authored-by: ivan-lemiashonak <[email protected]>
  • Loading branch information
ilemiashonak and ilemiashonak authored Sep 14, 2022
1 parent 5ffa816 commit 12cefe4
Show file tree
Hide file tree
Showing 8 changed files with 2,612 additions and 466 deletions.
84 changes: 36 additions & 48 deletions language/.generator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@ const yaml = require("yaml");
const Alpha2KeyName = "alpha2"
const Alpha3KeyName = "alpha3-b"
const LanguageNameKey = "English"
const options = {
hostname: "datahub.io",
port: 443,
path: "/core/language-codes/r/language-codes-3b2.json",
method: "GET",
headers: {
"Content-Type": "application/json"
},
};
const maxDownloadSize = 1000000 // 1Mb

getLanguages(options, maxDownloadSize)
getData("https://datahub.io/core/language-codes/r/language-codes-3b2.json", maxDownloadSize)
.then((codes) => {
let languages = prepareLanguageCodes(codes);
return Promise.all(prepareFilesContent(languages))
Expand All @@ -26,45 +17,42 @@ getLanguages(options, maxDownloadSize)
})
.catch((err) => console.error("woooooooops, seems like an error occurred...\n", err))

function getLanguages(options, maxDownloadSize) {
return new Promise((resolve, reject) => {

const req = https.request(options, res => {
let body = "", alreadyDownloaded = 0;

res.on("data", chunk => {
alreadyDownloaded += chunk.length;

if (alreadyDownloaded > maxDownloadSize) {
reject(`max download size of ${maxDownloadSize} bytes reached. it's better to search for less overhead data source`);
return
}
function getLanguages(url, maxSize, resolve, reject) {
https.get(url, (res) => {
if(res.statusCode === 301 || res.statusCode === 302) {
return getLanguages(res.headers.location, maxSize, resolve, reject)
}

body += chunk;
});
if (res.statusCode !== 200) {
reject(`got ${res.statusCode} response status code`);
return
}

res.on("end", () => {
if (res.statusCode !== 200) {
reject(`got ${res.statusCode} response status code`);
return
}
let body = [], alreadyDownloaded = 0;

if (alreadyDownloaded > maxDownloadSize) {
return
}
res.on("data", (chunk) => {
alreadyDownloaded += chunk.length;

const languagesResponse = JSON.parse(body);
if (alreadyDownloaded > maxDownloadSize) {
reject(`max download size of ${maxDownloadSize} bytes reached. it's better to search for less overhead data source`);
return
}

resolve(languagesResponse);
});
body.push(chunk);
});

req.on("error", error => {
reject(error)
});
res.on("end", () => {
try {
resolve(JSON.parse(Buffer.concat(body).toString()));
} catch (err) {
reject(err);
}
});
});
}

req.end();
})
async function getData(url, maxSize) {
return new Promise((resolve, reject) => getLanguages(url, maxSize, resolve, reject))
}

function prepareLanguageCodes(codes) {
Expand Down Expand Up @@ -140,7 +128,7 @@ import "github.com/mikekonan/go-types/v2/language"
const (
${codes.map(
(code) => `\t// ${code.a2.value} represents '${code.a2.value}' country alpha-2 code
${code.a2.value} = language.Alpha2Code("${code.a2.value}")`
${code.a2.value.toUpperCase()} = language.Alpha2Code("${code.a2.value}")`
).join("\n")}
)
`;
Expand All @@ -152,7 +140,7 @@ import "github.com/mikekonan/go-types/v2/language"
const (
${codes.map(
(code) => `\t// ${code.a3.value} represents '${code.a3.value}' country alpha-3 code
${code.a3.value} = language.Alpha3Code("${code.a3.value}")`
${code.a3.value.toUpperCase()} = language.Alpha3Code("${code.a3.value}")`
).join("\n")}
)
`;
Expand All @@ -179,23 +167,23 @@ const spec = {
LanguageName: {
example: "Norwegian",
type: "string",
format: "string",
format: "language-name",
enum: [...new Set(codes.map((code) => code.name.value))],
"x-go-type": "github.com/mikekonan/go-types/v2/language.Name",
},
CountryAlpha2: {
LanguageAlpha2: {
example: "no",
type: "string",
format: "ISO 639-1",
format: "iso639-1",
minLength: 2,
maxLength: 2,
enum: [...new Set(codes.map((code) => code.a2.value))],
"x-go-type": "github.com/mikekonan/go-types/v2/language.Alpha2Code",
},
CountryAlpha3: {
LanguageAlpha3: {
example: "nor",
type: "string",
format: "ISO 639-1",
format: "iso639-2",
minLength: 3,
maxLength: 3,
enum: [...new Set(codes.map((code) => code.a3.value))],
Expand Down
Loading

0 comments on commit 12cefe4

Please sign in to comment.