@@ -16,11 +16,11 @@ namespace Microsoft.ML.Data
16
16
/// </summary>
17
17
public sealed class MetadataBuilder
18
18
{
19
- private readonly List < ( string Name , ColumnType Type , Delegate Getter ) > _items ;
19
+ private readonly List < ( string Name , ColumnType Type , Delegate Getter , Schema . Metadata Metadata ) > _items ;
20
20
21
21
public MetadataBuilder ( )
22
22
{
23
- _items = new List < ( string Name , ColumnType Type , Delegate Getter ) > ( ) ;
23
+ _items = new List < ( string Name , ColumnType Type , Delegate Getter , Schema . Metadata Metadata ) > ( ) ;
24
24
}
25
25
26
26
/// <summary>
@@ -40,7 +40,7 @@ public void Add(Schema.Metadata metadata, Func<string, bool> selector)
40
40
foreach ( var column in metadata . Schema )
41
41
{
42
42
if ( selector ( column . Name ) )
43
- _items . Add ( ( column . Name , column . Type , metadata . Getters [ column . Index ] ) ) ;
43
+ _items . Add ( ( column . Name , column . Type , metadata . Getters [ column . Index ] , column . Metadata ) ) ;
44
44
}
45
45
}
46
46
@@ -51,13 +51,17 @@ public void Add(Schema.Metadata metadata, Func<string, bool> selector)
51
51
/// <param name="name">The metadata name.</param>
52
52
/// <param name="type">The metadata type.</param>
53
53
/// <param name="getter">The getter delegate.</param>
54
- public void Add < TValue > ( string name , ColumnType type , ValueGetter < TValue > getter )
54
+ /// <param name="metadata">Metadata of the input column. Note that metadata on a metadata column is somewhat rare
55
+ /// except for certain types (for example, slot names for a vector, key values for something of key type).</param>
56
+ public void Add < TValue > ( string name , ColumnType type , ValueGetter < TValue > getter , Schema . Metadata metadata = null )
55
57
{
56
58
Contracts . CheckNonEmpty ( name , nameof ( name ) ) ;
57
59
Contracts . CheckValue ( type , nameof ( type ) ) ;
58
60
Contracts . CheckValue ( getter , nameof ( getter ) ) ;
59
- Contracts . CheckParam ( type . RawType == typeof ( TValue ) , nameof ( getter ) ) ;
60
- _items . Add ( ( name , type , getter ) ) ;
61
+ Contracts . CheckParam ( type . RawType == typeof ( TValue ) , nameof ( type ) ) ;
62
+ Contracts . CheckValueOrNull ( metadata ) ;
63
+
64
+ _items . Add ( ( name , type , getter , metadata ) ) ;
61
65
}
62
66
63
67
/// <summary>
@@ -67,11 +71,31 @@ public void Add<TValue>(string name, ColumnType type, ValueGetter<TValue> getter
67
71
/// <param name="type">The metadata type.</param>
68
72
/// <param name="getter">The getter delegate that provides the value. Note that the type of the getter is still checked
69
73
/// inside this method.</param>
70
- public void Add ( string name , ColumnType type , Delegate getter )
74
+ /// <param name="metadata">Metadata of the input column. Note that metadata on a metadata column is somewhat rare
75
+ /// except for certain types (for example, slot names for a vector, key values for something of key type).</param>
76
+ public void Add ( string name , ColumnType type , Delegate getter , Schema . Metadata metadata = null )
71
77
{
72
78
Contracts . CheckNonEmpty ( name , nameof ( name ) ) ;
73
79
Contracts . CheckValue ( type , nameof ( type ) ) ;
74
- Utils . MarshalActionInvoke ( AddDelegate < int > , type . RawType , name , type , getter ) ;
80
+ Contracts . CheckValueOrNull ( metadata ) ;
81
+ Utils . MarshalActionInvoke ( AddDelegate < int > , type . RawType , name , type , getter , metadata ) ;
82
+ }
83
+
84
+ /// <summary>
85
+ /// Add one metadata column for a primitive value type.
86
+ /// </summary>
87
+ /// <param name="name">The metadata name.</param>
88
+ /// <param name="type">The metadata type.</param>
89
+ /// <param name="value">The value of the metadata.</param>
90
+ /// <param name="metadata">Metadata of the input column. Note that metadata on a metadata column is somewhat rare
91
+ /// except for certain types (for example, slot names for a vector, key values for something of key type).</param>
92
+ public void AddPrimitiveValue < TValue > ( string name , PrimitiveType type , TValue value , Schema . Metadata metadata = null )
93
+ {
94
+ Contracts . CheckNonEmpty ( name , nameof ( name ) ) ;
95
+ Contracts . CheckValue ( type , nameof ( type ) ) ;
96
+ Contracts . CheckParam ( type . RawType == typeof ( TValue ) , nameof ( type ) ) ;
97
+ Contracts . CheckValueOrNull ( metadata ) ;
98
+ Add ( name , type , ( ref TValue dst ) => dst = value , metadata ) ;
75
99
}
76
100
77
101
/// <summary>
@@ -100,19 +124,19 @@ public Schema.Metadata GetMetadata()
100
124
{
101
125
var builder = new SchemaBuilder ( ) ;
102
126
foreach ( var item in _items )
103
- builder . AddColumn ( item . Name , item . Type , null ) ;
127
+ builder . AddColumn ( item . Name , item . Type , item . Metadata ) ;
104
128
return new Schema . Metadata ( builder . GetSchema ( ) , _items . Select ( x => x . Getter ) . ToArray ( ) ) ;
105
129
}
106
130
107
- private void AddDelegate < TValue > ( string name , ColumnType type , Delegate getter )
131
+ private void AddDelegate < TValue > ( string name , ColumnType type , Delegate getter , Schema . Metadata metadata )
108
132
{
109
133
Contracts . AssertNonEmpty ( name ) ;
110
134
Contracts . AssertValue ( type ) ;
111
135
Contracts . AssertValue ( getter ) ;
112
136
113
137
var typedGetter = getter as ValueGetter < TValue > ;
114
138
Contracts . CheckParam ( typedGetter != null , nameof ( getter ) ) ;
115
- _items . Add ( ( name , type , typedGetter ) ) ;
139
+ _items . Add ( ( name , type , typedGetter , metadata ) ) ;
116
140
}
117
141
}
118
142
}
0 commit comments