Skip to content

Conversation

Actimel
Copy link
Contributor

@Actimel Actimel commented Oct 14, 2018

Solve #46

@Actimel Actimel requested a review from tomasfejfar October 14, 2018 08:31
@Actimel Actimel self-assigned this Oct 14, 2018
@Actimel
Copy link
Contributor Author

Actimel commented Oct 14, 2018

Nastrelil jsem zatim getter na state file.

IMHO by stalo za to dodelat tam i metodu na zapis do .../out/state.json (#47), co vy na to?

protected function writeOutputStateToFile(array $state): void
{
$outDataDir = $this->getDataDir() . '/out';
if (!is_dir($outDataDir)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pryc s tim, adresar existuje

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V testech by nemusel, teoreticky. Spíš co použít SF Filesystem?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tam by mělo stačit $fs->dump($cesta, $obsah)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no tak snad nemusime psat kod kvuli tomu, aby prochazely testy, ktery neodpovidaji realite :) fs->dump beru, ale stejne tak beru ze to vyleti s unhandled exception, protoze to proste je neocekavana situace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, vyhodim to, u testu problem nevidim, jednoduse tam tu slozku budeme mit uz udelanou.

$logger = new Logger();
$baseComponent = new BaseComponent($logger);

$this->assertEmpty($baseComponent->getInputState());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertSame([] ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je potreba byt az tak paranoidni? getInputState ma return type array a i kdyz ho nejakym zpusobem zmenis, tak se to vysype jinde.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ne to neni kvuli paranoi, ale kvuli specifikaci. tj.

Pokud soubor neexistuje, tak state je prazdne pole

tak testujme specifikaci ^ a ne neco jinyho.

$jsonEncoder = new JsonEncoder();
$this->inputState = $jsonEncoder->decode($jsonContents, JsonEncoder::FORMAT);
} else {
$this->inputState = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tady je jeden potencionalni problem, kdyz bude ve state {} nebo [] a prozene se to pres assoc decoder, takze z toho vzdycky vznikne []. Vzhledem k tomu, ze state jsou interni data jedne aplikace a ta aplikace je vzdycky v PHP, tak si myslim, ze to vubec nevadi. Presto jsem to sem napsal, abyste se nad tim s @tomasfejfar zamysleli jestli vas nenapadne sitauce ve ktere by to mohl byt problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@odinuv Kdyz bude ve state {}/[], tak je to uplne jedno, oboji je validni prazdny json. To by nemelo vadit nikde.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ja vim, ze je oboji validni, jde o to, ze se prazdny objekty konvertuji na pole a nelze rozlisit mezi tim jestli to byl prazdny objekt nebo prazdne pole
python by se na tom treba vysypal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To uz je pak ale na pythonovou komponentu. Jak si psal toto je vstup pouze pro php, takze je to jedno. Na vystupu nikdy prazdny state zapisovat nebudeme, coz bych videl jeko potencialni nekonzistenci.

{
$inputStateFile = $this->getDataDir() . '/in/state.json';
if (file_exists($inputStateFile)) {
$jsonContents = (string) file_get_contents($inputStateFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Já bych, když se nám to tu už objevuje víckrát, extrahoval tuhle funkčnost do samostatné třídy s tím, že tam bude všechno - kontrola, jestli soubor existuje, správné natažení ze souboru, decodování přeš JsonEncoder, atp. Teď jsem zrovna potřeboval natahnout JSON ze souboru a musel jsem to zas napsat celé znova, takže jsem se na to vyprd a udělal jsem jen json_decode(file_get_contents())...

protected function writeOutputStateToFile(array $state): void
{
$outDataDir = $this->getDataDir() . '/out';
if (!is_dir($outDataDir)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V testech by nemusel, teoreticky. Spíš co použít SF Filesystem?

protected function writeOutputStateToFile(array $state): void
{
$outDataDir = $this->getDataDir() . '/out';
if (!is_dir($outDataDir)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tam by mělo stačit $fs->dump($cesta, $obsah)

}

$outputStateFile = $outDataDir . '/state.json';
$jsonEncode = new JsonEncoder();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tohle je to samé - mohlo by se hodit to mít např. pro zápis manifestu tabulek - JsonReader & JsonWriter... a ošetřit situace, kdy nejde encodovat, neexistuje složka s tím souborem, atp.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a ošetřit situace, kdy nejde encodovat, neexistuje složka s tím souborem,

je to app error, nemusi se to osetrovat

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomasfejfar Na to co tu popisujes budou stacit 2 metody, nebude jednodussi udelat nejaky JsonFileHelper, ktery bude umet oboji zaroven?
@odinuv Sice to vyhodi app error, ale nekde s temi stavy budes chtit pracovat, napr. zrovna tady s tim stavem, kdy state.json neexistuje, tak chces nastavit $state na [].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jeste k tomu teda jedna vec. Opravdu tady potrebujeme SF/serializer? Serializovani niceho jineho tu ani nepotrebujeme a na json nam bohate staci json_decode/json_encode

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jedinej stav se kterym tu chces pracovat je, ten, ze ten soubor neexistuje, vsechno ostatni je app error, takze netreba overengineering

taky si myslim, ze by tu bohate stacil json_encode a json_decode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tak uz mame 7.2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, tak aspoň ošetření přes json_last_error. Protože pořád platí, že:

json_decode('null') === json_decode('{invalidJson');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A ano, klidne to může být jen jedna třída. Ale mělo by to být víc než jen metoda - aby to bylo otestované a dalo se to použít kdykoli - třeba v testu.

@Actimel Actimel force-pushed the david-get-state branch 2 times, most recently from 105ef2d to 1164135 Compare October 15, 2018 19:29
(new JsonFileHelper())->write(
$this->getDataDir() . '/out/state.json',
$state,
['json_encode_options' => JSON_PRETTY_PRINT]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nehodilo by se to jako default? Jak často zapisujeme json neformátovaný? Osobně bych radši viděl true/false parametr, protože posílat celý kontext mi přijde potenciálně problematické.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nekde jsem neformatovany json uz videl - myslim, ze v teste db-ex-common/wr-bigquery. Nicmene zrovna na tech mistech mi prijde lepsi to mit naformatovane, at se v tom clovek dokaze jednoduze zorientovat.

Mozna nemusime posilat cely kontext, ale primo json_encode_options , tim budes schopny ten json dodatecne nastavit. Nenapada me ale pripad, kdy pouzivame neco jine nez JSON_PRETTY_PRINT.

Jestli vas taky nic nenapada tak to muzeme resit true/false parametrem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spis je mozna otazka - potrebujem tam to nastaveni vubec na neco? jestli jo, tak bych to dal jako default a bool, nic jinyho jsme nikdy nepotrebovali (a kdyby to nekdy nekdo potrevoval, tak se to da vzdycky napsal low-level)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

osobne bych to teda vubec nedaval nastavitelny, at je to co nejjednodussi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pridal jsem tam ten bool, a defaultne to nastavil na true. Kdyz to budes chtit vypsat na jeden radek, musis tam dopsat false.

Copy link
Member

@odinuv odinuv Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, co kdyby to byly staticky metody? helper zadnej vnitrni stav nema a mit nebude a
JsonFileHelper::write(...) je jednodussi nez (new JsonFileHelper())->write(...)

file_get_contents($filePath)
);
unlink($filePath);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chybí test writeToDirectoryThatDoesNotExist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doplnil jsem.

unlink($filePath);
}

public function testWriteToNonExistingDirectoryThrowsException(): void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tohle podle mého je nesprávné chování - právě, že by to mělo šetřit čas a tu složku vygenerovat. Co, @odinuv ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@odinuv Psal, ze ta slozka out, tam vzdy bude a kdyz ne, tak se to ma probublat do ApplicationErroru.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zalezi za co chcete ten jsonfilehelper mit, jestli ma slouzit jen pro potreby php-component, tak to slozku vytvaret nemusi
jestli ho chcete pouzivat i jinde, tak je to na zvazeni

Copy link
Contributor

@tomasfejfar tomasfejfar Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Není to statefilewriter. Je to generický dumper json filů. Takže s tím stejně tak dobře můžu chtít zapsat třeba /out/tables/mytable/some.manifest

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nemohli bysme tady v tomhle PR nechat symfony encoder a ten helper resit nekde zvlast?
protoze kdyz tohle mergnem, tak bude state pouzivat helper, a manifset symfony encoder, anebo to teda zmenit i tam, ale to u zase dela ten PR 3 veci

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ale jo, dejme to tam...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

taky nemam php-utils rad a dal bych to radsi nekam jinam

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toho jsem si take vsiml, proto bych to zadelal do slozky, at je to od toho pelmelu odstinene.

Ale lepsi nez se tomu php-utils vyhybat jak cert krizi protoze je v nem chaoz, bychom mu mohli timto dat impuls k oziveni. Nez se na to divat jak to hnije.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oziveni php-utils vyzaduje dohledat kde vsude se ty funkce tam definovany opravdu pouzivaji a potom s tim nejak nalozit, tipuju, ze se to cely pouziva tak ve 3 appkach a kazda z toho pouziva uplne disjunktni mnozinu funkci :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pridal jsem vytvareni slozky, pokud neexistuje a predelal metody na staticke.

Muzeme to takto mergnout? To do jakych utils vytahnout ten helper bych resil potom.

}

$jsonEncoder = new JsonEncoder();
file_put_contents(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tady by to melo vyhodit vyjimku pokud je navratova hodnota === false, jinak to nemuselo zapsat

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vis za jakych podminek to vraci false? Nenapada me jak to otestovat.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tohle se blbe testuje :/, mozna by slo neco jako zapis do /dev/full nebo nejaky invalid path nebo php://stdin ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nebo adresar, co nema write permissions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To jsem zkousel, ale kdyz nemas write permission, tak uz sam file_put_contents vyhodi exception. Zatim to nechavam bez testu.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public function testWriteToDevFull(): void
    {
        $filePath = '/dev/full';
        $array = ['key'];

        JsonFileHelper::write($filePath, $array);
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protože se to nastavuje i v tom bootstrapu testů.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jasne, to je ono. Jinak je to warning. Nicmene i tak nevim, jak otestovat, kdy file_put_content vraci false 😕

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v php-component nijak, tady to vyhazuje vyjimku, takze tady staci otestovat, ze to vyhazuje vyjimku, az se to bude davat do utils, tak tam je to potreba poresit - bud registraci handleru, nebo @ a otestovani false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, tak tu jsem pridal ten test.

Copy link
Member

@odinuv odinuv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ja bych bylo pro to mergnout, ale pokud ano tak soucasne s vytvorenim issue na pouziti JsonFileHelper v manifestech, at je vsude stejny encoder

Copy link
Member

@odinuv odinuv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beru zpet

Copy link
Member

@odinuv odinuv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boha jeho, jinej PR, sorry!

@Actimel
Copy link
Contributor Author

Actimel commented Oct 17, 2018

Zalozil jsem #49

@Actimel Actimel merged commit 5c7c604 into master Oct 17, 2018
@Actimel Actimel deleted the david-get-state branch October 17, 2018 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants