test with nest union
This commit is contained in:
parent
dd65178724
commit
6c0fbc4bbd
3 changed files with 51 additions and 25 deletions
|
|
@ -20,6 +20,13 @@ struct Nulldata $C.mapname("nulldata_t") {
|
|||
null @0: UInt32 $C.mapname("null_");
|
||||
}
|
||||
|
||||
struct Buy $C.mapname("buy_t") {
|
||||
from @0: Text;
|
||||
u :union $C.mapname("u") $C.mapuniontag("with_recipe") {
|
||||
norecipe @1: Void;
|
||||
recipeAddr @2: Text $C.mapname("recipe_addr");
|
||||
}
|
||||
}
|
||||
struct Book $C.mapname("book_t") {
|
||||
title @0: Text;
|
||||
authors @1: List(Text) $C.mapname("authors") $C.maplistcount("n_authors");
|
||||
|
|
@ -29,7 +36,7 @@ struct Book $C.mapname("book_t") {
|
|||
magic1 @2: List(UInt32) $C.mapname("magic_1") $C.maplistcount("n_magic1");
|
||||
description @8: Text;
|
||||
acquire :union $C.mapuniontag("acquire_method") {
|
||||
buy @3: Text;
|
||||
buy @3: Buy;
|
||||
donation @4: Text;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,36 +6,44 @@
|
|||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
char *caption;
|
||||
int32_t start;
|
||||
int32_t end;
|
||||
char *caption;
|
||||
int32_t start;
|
||||
int32_t end;
|
||||
} chapter_t;
|
||||
|
||||
typedef struct {
|
||||
uint64_t isbn;
|
||||
uint32_t year;
|
||||
uint64_t isbn;
|
||||
uint32_t year;
|
||||
} publish_t;
|
||||
|
||||
typedef struct {
|
||||
int null_;
|
||||
int null_;
|
||||
} nulldata_t;
|
||||
|
||||
typedef struct {
|
||||
char *title;
|
||||
int n_authors;
|
||||
char **authors;
|
||||
int n_chapters;
|
||||
chapter_t **chapters_;
|
||||
publish_t *publish;
|
||||
nulldata_t *nulldata;
|
||||
int n_magic1;
|
||||
uint32_t *magic_1;
|
||||
char *description;
|
||||
int acquire_method;
|
||||
union {
|
||||
char *buy;
|
||||
char *donation;
|
||||
} acquire;
|
||||
char *from;
|
||||
int with_recipe;
|
||||
union {
|
||||
char *recipe_addr;
|
||||
} u;
|
||||
} buy_t;
|
||||
|
||||
typedef struct {
|
||||
char *title;
|
||||
int n_authors;
|
||||
char **authors;
|
||||
int n_chapters;
|
||||
chapter_t **chapters_;
|
||||
publish_t *publish;
|
||||
nulldata_t *nulldata;
|
||||
int n_magic1;
|
||||
uint32_t *magic_1;
|
||||
char *description;
|
||||
int acquire_method;
|
||||
union {
|
||||
buy_t *buy;
|
||||
char *donation;
|
||||
} acquire;
|
||||
} book_t;
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -38,6 +38,13 @@ int encode() {
|
|||
.isbn = 335677,
|
||||
.year =2001
|
||||
};
|
||||
buy_t buy = {
|
||||
.from = "Xinghua Book store",
|
||||
.with_recipe = Buy_u_recipeAddr,
|
||||
.u = {
|
||||
.recipe_addr = "Xinghua Street"
|
||||
}
|
||||
};
|
||||
struct capn_segment *cs;
|
||||
struct Book b;
|
||||
Book_ptr p;
|
||||
|
|
@ -53,7 +60,7 @@ int encode() {
|
|||
book.magic_1 = &magic1[0];
|
||||
book.description = NULL;
|
||||
book.acquire_method = Book_acquire_buy;
|
||||
book.acquire.buy = "bought from Xinhua book store";
|
||||
book.acquire.buy = &buy;
|
||||
|
||||
capn_init_malloc(&c);
|
||||
cs = capn_root(&c).seg;
|
||||
|
|
@ -106,7 +113,11 @@ int decode() {
|
|||
}
|
||||
|
||||
if (book->acquire_method == Book_acquire_buy) {
|
||||
printf("%s\n", book->acquire.buy);
|
||||
printf("buy from %s\n", book->acquire.buy->from);
|
||||
if (book->acquire.buy->with_recipe == Buy_u_recipeAddr) {
|
||||
printf("recipe address %s\n",
|
||||
book->acquire.buy->u.recipe_addr);
|
||||
}
|
||||
}
|
||||
else {
|
||||
printf("%s\n", book->acquire.donation);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue