-
Notifications
You must be signed in to change notification settings - Fork 1
Add loading input state file #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Nastrelil jsem zatim getter na state file. IMHO by stalo za to dodelat tam i metodu na zapis do |
src/BaseComponent.php
Outdated
protected function writeOutputStateToFile(array $state): void | ||
{ | ||
$outDataDir = $this->getDataDir() . '/out'; | ||
if (!is_dir($outDataDir)) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
tests/BaseComponentTest.php
Outdated
$logger = new Logger(); | ||
$baseComponent = new BaseComponent($logger); | ||
|
||
$this->assertEmpty($baseComponent->getInputState()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertSame([] ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 = []; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
src/BaseComponent.php
Outdated
{ | ||
$inputStateFile = $this->getDataDir() . '/in/state.json'; | ||
if (file_exists($inputStateFile)) { | ||
$jsonContents = (string) file_get_contents($inputStateFile); |
There was a problem hiding this comment.
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())...
src/BaseComponent.php
Outdated
protected function writeOutputStateToFile(array $state): void | ||
{ | ||
$outDataDir = $this->getDataDir() . '/out'; | ||
if (!is_dir($outDataDir)) { |
There was a problem hiding this comment.
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?
src/BaseComponent.php
Outdated
protected function writeOutputStateToFile(array $state): void | ||
{ | ||
$outDataDir = $this->getDataDir() . '/out'; | ||
if (!is_dir($outDataDir)) { |
There was a problem hiding this comment.
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)
src/BaseComponent.php
Outdated
} | ||
|
||
$outputStateFile = $outDataDir . '/state.json'; | ||
$jsonEncode = new JsonEncoder(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 []
.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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');
There was a problem hiding this comment.
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.
105ef2d
to
1164135
Compare
1164135
to
d47bbd8
Compare
src/BaseComponent.php
Outdated
(new JsonFileHelper())->write( | ||
$this->getDataDir() . '/out/state.json', | ||
$state, | ||
['json_encode_options' => JSON_PRETTY_PRINT] |
There was a problem hiding this comment.
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é.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chybí test writeToDirectoryThatDoesNotExist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doplnil jsem.
75997d7
to
0ed97bd
Compare
0ed97bd
to
a945165
Compare
tests/JsonFileHelperTest.php
Outdated
unlink($filePath); | ||
} | ||
|
||
public function testWriteToNonExistingDirectoryThrowsException(): void |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
src/JsonFileHelper.php
Outdated
} | ||
|
||
$jsonEncoder = new JsonEncoder(); | ||
file_put_contents( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
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ů.
There was a problem hiding this comment.
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 😕
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beru zpet
There was a problem hiding this 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!
Zalozil jsem #49 |
Solve #46