github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/appdef/example_test.go (about) 1 /* 2 * Copyright (c) 2021-present Sigma-Soft, Ltd. 3 * @author: Nikolay Nikitin 4 */ 5 6 package appdef_test 7 8 import ( 9 "fmt" 10 11 "github.com/voedger/voedger/pkg/appdef" 12 ) 13 14 func Example() { 15 16 var app appdef.IAppDef 17 docName, recName := appdef.NewQName("test", "doc"), appdef.NewQName("test", "rec") 18 19 // how to build AppDef with CDoc 20 { 21 adb := appdef.New() 22 adb.AddPackage("test", "test.com/test") 23 24 doc := adb.AddCDoc(docName) 25 doc.SetComment("This is example doc") 26 doc. 27 AddField("f1", appdef.DataKind_int64, true).SetFieldComment("f1", "Field may have comments too"). 28 AddField("f2", appdef.DataKind_string, false) 29 rec := adb.AddCRecord(recName) 30 31 doc.AddContainer("rec", recName, 0, appdef.Occurs_Unbounded) 32 33 rec. 34 AddField("f1", appdef.DataKind_int64, true). 35 AddField("f2", appdef.DataKind_string, false) 36 37 app = adb.MustBuild() 38 } 39 40 // how to inspect builded AppDef with CDoc 41 { 42 // how to find type by name 43 t := app.Type(docName) 44 fmt.Printf("type %q: %v\n", t.QName(), t.Kind()) 45 46 // how to cast type to cdoc 47 d, ok := t.(appdef.ICDoc) 48 fmt.Printf("%q is CDoc: %v\n", d.QName(), ok && (d.Kind() == appdef.TypeKind_CDoc)) 49 50 // how to find CDoc by name 51 doc := app.CDoc(docName) 52 fmt.Printf("doc %q: %v. %s\n", doc.QName(), doc.Kind(), d.Comment()) 53 54 // how to inspect doc fields 55 fmt.Printf("doc field count: %v\n", doc.UserFieldCount()) 56 57 fmt.Println("founded", doc.Field("f1")) 58 59 fldCnt := 0 60 for _, f := range doc.Fields() { 61 fldCnt++ 62 if f.IsSys() { 63 fmt.Print("*") 64 } else { 65 fmt.Print(" ") 66 } 67 info := fmt.Sprintf("%d. %v, required: %v", fldCnt, f, f.Required()) 68 if f.Comment() != "" { 69 info += ". " + f.Comment() 70 } 71 fmt.Println(info) 72 } 73 74 // how to inspect doc containers 75 fmt.Printf("doc container count: %v\n", doc.ContainerCount()) 76 77 fmt.Println("founded", doc.Container("rec")) 78 79 contCnt := 0 80 for _, c := range doc.Containers() { 81 contCnt++ 82 fmt.Printf("%d. %v, occurs: %v…%v\n", contCnt, c, c.MinOccurs(), c.MaxOccurs()) 83 } 84 85 // what if unknown type 86 fmt.Println("unknown type:", app.Type(appdef.NewQName("test", "unknown"))) 87 } 88 89 // Output: 90 // type "test.doc": TypeKind_CDoc 91 // "test.doc" is CDoc: true 92 // doc "test.doc": TypeKind_CDoc. This is example doc 93 // doc field count: 2 94 // founded int64-field «f1» 95 // *1. QName-field «sys.QName», required: true 96 // *2. RecordID-field «sys.ID», required: true 97 // *3. bool-field «sys.IsActive», required: false 98 // 4. int64-field «f1», required: true. Field may have comments too 99 // 5. string-field «f2», required: false 100 // doc container count: 1 101 // founded container «rec: test.rec» 102 // 1. container «rec: test.rec», occurs: 0…unbounded 103 // unknown type: null type 104 }