jockchou / pb4php

Automatically exported from code.google.com/p/pb4php
1 stars 0 forks source link

_check_type() doesn't seem to handle namespace properly #14

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The _check_type() method doesn't seem to be handling namespace properly.

Example proto file:
message BlahClass {
    option string blah = 1;
}

message TestClass {
   message TestGroup {
       optional BlahClass blah =1;
   }
}

Result:
This throws the exception "Protofile type BlahClass is unknown!"

Expected:
BlahClass was defined as the top level namespace and should not have thrown
this error.

I replaced the __check_type() function with this code to fix this issue:
    private function _check_type($type, $array, $path)
    {
        if (isset($this->scalar_types[strtolower($type)]))
            return array(strtolower($type), '');

        $apath = explode(".", $path);
        while ( array_pop($apath ) ) {
            $namespace = join(".", $apath) . "." . $type;

            foreach ($this->m_types as $message)
            {
                if ($message['name'] == $namespace)
                {
                    return array($type, $namespace);
                }
            }
        }

        $namespace = $type;
        foreach ($this->m_types as $message)
        {
            if ($message['name'] == $namespace)
            {
                return array($type, $namespace);
            }
        }

        // @TODO TYPE CHECK
        throw new Exception('Protofile type ' . $type . ' unknown!');
    }

Original issue reported on code.google.com by mrst...@gmail.com on 13 Mar 2010 at 12:08

GoogleCodeExporter commented 8 years ago
I had an issue with nested class, 3 level deep. The code worked with 2 level or 
less. I simply changed your code to use a do while instead of a while loop to 
fix the problem:
    private function _check_type($type, $array, $path)
    {
        if (isset($this->scalar_types[strtolower($type)]))
            return array(strtolower($type), '');

        $apath = explode(".", $path);
        do {
            $namespace = join(".", $apath) . "." . $type;

            foreach ($this->m_types as $message)
            {
                if ($message['name'] == $namespace)
                {
                    return array($type, $namespace);
                }
            }
        } while ( array_pop($apath ) );

        $namespace = $type;
        foreach ($this->m_types as $message)
        {
            if ($message['name'] == $namespace)
            {
                return array($type, $namespace);
            }
        }

        // @TODO TYPE CHECK
        throw new Exception('Protofile type ' . $type . ' unknown!');
    }

Thanks for your function.

Original comment by benoit.a...@gmail.com on 15 Sep 2010 at 3:51