github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/appdef/example_structures_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 ExampleIAppDef_Structures() {
    15  
    16  	var app appdef.IAppDef
    17  	docName, recName := appdef.NewQName("test", "document"), appdef.NewQName("test", "record")
    18  	objName, childName := appdef.NewQName("test", "object"), appdef.NewQName("test", "child")
    19  
    20  	// how to build AppDef with structures
    21  	{
    22  		adb := appdef.New()
    23  		adb.AddPackage("test", "test.com/test")
    24  
    25  		doc := adb.AddCDoc(docName)
    26  		doc.
    27  			AddField("f1", appdef.DataKind_int64, true).
    28  			AddField("f2", appdef.DataKind_string, false)
    29  		doc.AddContainer("rec", recName, 0, appdef.Occurs_Unbounded)
    30  
    31  		rec := adb.AddCRecord(recName)
    32  		rec.
    33  			AddField("f1", appdef.DataKind_int64, true).
    34  			AddField("f2", appdef.DataKind_string, false)
    35  
    36  		obj := adb.AddObject(objName)
    37  		obj.
    38  			AddField("f1", appdef.DataKind_int64, true).
    39  			AddField("f2", appdef.DataKind_string, false)
    40  		obj.AddContainer("child", childName, 0, appdef.Occurs_Unbounded)
    41  
    42  		child := adb.AddObject(childName)
    43  		child.
    44  			AddField("f1", appdef.DataKind_int64, true).
    45  			AddField("f2", appdef.DataKind_string, false)
    46  
    47  		app = adb.MustBuild()
    48  	}
    49  
    50  	// how to inspect builded AppDef with structures
    51  	{
    52  		// how to enum structures
    53  		cnt := 0
    54  		app.Structures(func(s appdef.IStructure) {
    55  			cnt++
    56  			fmt.Printf("%d. %v\n", cnt, s)
    57  			fmt.Printf("- user/overall field count: %d/%d\n", s.UserFieldCount(), s.FieldCount())
    58  			fmt.Printf("- container count: %d\n", s.ContainerCount())
    59  		})
    60  		fmt.Printf("Overall %d structures\n", cnt)
    61  
    62  		// how to find structure by name
    63  		fmt.Println(app.Structure(docName))
    64  		fmt.Println(app.Structure(appdef.NewQName("test", "unknown")))
    65  	}
    66  
    67  	// how to inspect builded AppDef with records
    68  	{
    69  		cnt := 0
    70  		app.Records(func(s appdef.IRecord) {
    71  			cnt++
    72  			fmt.Printf("%d. %v\n", cnt, s)
    73  			fmt.Printf("- user/overall field count: %d/%d\n", s.UserFieldCount(), s.FieldCount())
    74  			fmt.Printf("- container count: %d\n", s.ContainerCount())
    75  		})
    76  
    77  		fmt.Printf("Overall %d records\n", cnt)
    78  
    79  		fmt.Println(app.Record(recName), app.Record(appdef.NewQName("test", "unknown")))
    80  	}
    81  
    82  	// Output:
    83  	// 1. Object «test.child»
    84  	// - user/overall field count: 2/4
    85  	// - container count: 0
    86  	// 2. CDoc «test.document»
    87  	// - user/overall field count: 2/5
    88  	// - container count: 1
    89  	// 3. Object «test.object»
    90  	// - user/overall field count: 2/4
    91  	// - container count: 1
    92  	// 4. CRecord «test.record»
    93  	// - user/overall field count: 2/7
    94  	// - container count: 0
    95  	// Overall 4 structures
    96  	// CDoc «test.document»
    97  	// <nil>
    98  	// 1. CDoc «test.document»
    99  	// - user/overall field count: 2/5
   100  	// - container count: 1
   101  	// 2. CRecord «test.record»
   102  	// - user/overall field count: 2/7
   103  	// - container count: 0
   104  	// Overall 2 records
   105  	// CRecord «test.record» <nil>
   106  }