示例#1
0
    def write_parse_method(self, enum, file, m):
        fmt = m.import_("fmt")

        @file.func(goname("Parse" + titleize(enum.name)),
                   args=enum.type(enum.shortname),
                   returns=enum,
                   comment="parse",
                   nostore=True)
        def parse(m):
            s = enum.shortname
            with m.switch(s) as sw:
                for name, _, value, _ in enum.members.values():
                    with sw.case(tostring(value)):
                        sw.return_(enum.varname(name))
                with sw.default():
                    sw.stmt('panic({})'.format(
                        fmt.Sprintf(
                            "unexpected {} %v, in parse()".format(enum.name),
                            _noencoded(s))))
            return m

        self.writer.write_function(parse, file, m)
示例#2
0
 def varname(self, name):
     return goname("{}{}".format(self.name, titleize(name)))