{"action":"created","issue":{"url":"https://api.github.com/repos/construct/construct/issues/60","repository_url":"https://api.github.com/repos/construct/construct","labels_url":"https://api.github.com/repos/construct/construct/issues/60/labels{/name}","comments_url":"https://api.github.com/repos/construct/construct/issues/60/comments","events_url":"https://api.github.com/repos/construct/construct/issues/60/events","html_url":"https://github.com/construct/construct/issues/60","id":112708051,"number":60,"title":"Auto calculated size","user":{"login":"WoLfulus","id":412397,"avatar_url":"https://avatars.githubusercontent.com/u/412397?v=3","gravatar_id":"","url":"https://api.github.com/users/WoLfulus","html_url":"https://github.com/WoLfulus","followers_url":"https://api.github.com/users/WoLfulus/followers","following_url":"https://api.github.com/users/WoLfulus/following{/other_user}","gists_url":"https://api.github.com/users/WoLfulus/gists{/gist_id}","starred_url":"https://api.github.com/users/WoLfulus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WoLfulus/subscriptions","organizations_url":"https://api.github.com/users/WoLfulus/orgs","repos_url":"https://api.github.com/users/WoLfulus/repos","events_url":"https://api.github.com/users/WoLfulus/events{/privacy}","received_events_url":"https://api.github.com/users/WoLfulus/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2015-10-22T00:56:26Z","updated_at":"2016-09-04T01:25:58Z","closed_at":"2016-09-04T01:25:58Z","body":"Is there any way to automatically calculate the whole struct size?\r\n\r\nFor example, I have a struct like this:\r\n\r\n    int8 type; // header \r\n    intX size; // header\r\n    int8[] data;\r\n\r\n\"type\" can be anything from 0 to 2, in which\r\n\r\n0 makes size be an int8\r\n1 makes size be an int16\r\n2 makes size be an int32\r\n\r\n\"size\" will be calculated based on header length + data length\r\n\r\nI can parse this without problems with a switch do switch between int8/16/32 types of \"size\" and an anchor to retrieve the current header length... data will be Bytes() using context to calculate **header.size - header.length** - not sure if this is the right way to do it\r\n\r\nthe problem comes when I try to build an object using this method\r\n\r\nis there any way to make size calculate the whole struct size based on the whole struct size?\r\n\r\nThe current code I came up with to parse things:\r\n\r\n    Header = Struct(\"header\",\r\n        UBInt8(\"type\"),\r\n        Switch(\"size\", lambda ctx: ctx.type,\r\n        {\r\n            0: UBInt8(\"size\"),\r\n            1: UBInt16(\"size\"),\r\n            2: UBInt32(\"size\")\r\n        }),\r\n        Anchor(\"length\")\r\n    )\r\n    \r\n    HeaderData = Struct(\"header\",\r\n        Embed(Header),\r\n        Bytes(\"data\", lambda ctx: ctx.size - ctx.length)\r\n    )\r\n\r\nBuilding HeaderData will result in an invalid buffer(header)"},"comment":{"url":"https://api.github.com/repos/construct/construct/issues/comments/244578888","html_url":"https://github.com/construct/construct/issues/60#issuecomment-244578888","issue_url":"https://api.github.com/repos/construct/construct/issues/60","id":244578888,"user":{"login":"arekbulski","id":5385838,"avatar_url":"https://avatars.githubusercontent.com/u/5385838?v=3","gravatar_id":"","url":"https://api.github.com/users/arekbulski","html_url":"https://github.com/arekbulski","followers_url":"https://api.github.com/users/arekbulski/followers","following_url":"https://api.github.com/users/arekbulski/following{/other_user}","gists_url":"https://api.github.com/users/arekbulski/gists{/gist_id}","starred_url":"https://api.github.com/users/arekbulski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arekbulski/subscriptions","organizations_url":"https://api.github.com/users/arekbulski/orgs","repos_url":"https://api.github.com/users/arekbulski/repos","events_url":"https://api.github.com/users/arekbulski/events{/privacy}","received_events_url":"https://api.github.com/users/arekbulski/received_events","type":"User","site_admin":false},"created_at":"2016-09-04T01:25:58Z","updated_at":"2016-09-04T01:25:58Z","body":"If I didnt solve the problem for you, please reopen. Good luck."}}
{"actor":{"display_login":"arekbulski"}}