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