question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using variables from parent scope in a 'choice'

See original GitHub issue

Hey Keichi

I was wondering if it’s somehow possible to use a variable from a parent scope from inside a choice type item. If it’s not implemented I would greatly appreciate a pointer on how to do so. My use-case is parsing of a DNSRecord:

var TXTRecord = new Parser()
    .string('txt' { length: 'rdlength' }) // rdlength is undefined

var answer = new Parser()
    .nest('name', { type: fqdn_pretty })
    .uint16be('type')
    .uint16be('class')
    .uint32be('ttl')
    .uint16be('rdlength')
    .choice('record', {
        tag: 'type',
        choices: {
            0x01: ARecord,
            0x02: PTRRecord, 0x05: PTRRecord, 0x0c: PTRRecord,
            0x10: TXTRecord,
            0x21: SRVRecord,
            0x1C: AAAARecord
        },
        defaultChoice: new Parser().buffer('rdata', { length: 'rdlength' }) //rdlength is undefined
    })

I enjoy working with your module, thanks for the help in advance!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
keichicommented, Jan 7, 2015

I understand your situation. Unfortunately, there’s currently no way to do that. One idea would be to introduce a special variable name like $parent, with which you can reference the parent scope: defaultChoice: new Parser().buffer('rdata', { length: '$parent.rdlength' }) What do you think about this?

0reactions
avi91commented, Jul 2, 2020

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

cmake - Variables set with PARENT_SCOPE are empty in the ...
If PARENT_SCOPE is present, the variable will be set in the scope above the current scope. Each new directory or function creates a...
Read more >
Setting CMake variables in the parent scope - Bastian Rieck
This option can be used in SET() statements of subordinate modules to set a variable in the scope of the calling module, i.e....
Read more >
about Scopes - PowerShell | Microsoft Learn
The calling scope is the parent scope. ... It includes all the variables that have the AllScope option, plus some automatic variables.
Read more >
Explanation of CMake variable scope , add_subdirectory vs ...
In this post, I will explain the scope of CMake variables with simple ... It sets variable A to “Parent” , then call...
Read more >
Some notes on CMake variables and scopes - Matthew Gaudet
Variables are set in CMake using set : ... Each scope when created maintains a link to its parent scope, and so you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found