@@ -43,6 +43,41 @@ const MAP_LOGICAL_TYPE: &str = "map";
4343// This const may better to maintain in avro-rs.
4444const LOGICAL_TYPE : & str = "logicalType" ;
4545
46+ fn literal_to_json ( literal : & crate :: spec:: Literal ) -> Result < Value > {
47+ match literal {
48+ crate :: spec:: Literal :: Primitive ( p) => match p {
49+ crate :: spec:: PrimitiveLiteral :: Boolean ( b) => Ok ( Value :: Bool ( * b) ) ,
50+ crate :: spec:: PrimitiveLiteral :: Int ( i) => Ok ( Value :: Number ( Number :: from ( * i) ) ) ,
51+ crate :: spec:: PrimitiveLiteral :: Long ( l) => Ok ( Value :: Number ( Number :: from ( * l) ) ) ,
52+ crate :: spec:: PrimitiveLiteral :: Float ( f) => Ok ( Value :: Number (
53+ Number :: from_f64 ( f. 0 as f64 ) . ok_or_else ( || {
54+ Error :: new (
55+ ErrorKind :: DataInvalid ,
56+ "Failed to convert float to json number" ,
57+ )
58+ } ) ?,
59+ ) ) ,
60+ crate :: spec:: PrimitiveLiteral :: Double ( d) => Ok ( Value :: Number (
61+ Number :: from_f64 ( d. 0 ) . ok_or_else ( || {
62+ Error :: new (
63+ ErrorKind :: DataInvalid ,
64+ "Failed to convert double to json number" ,
65+ )
66+ } ) ?,
67+ ) ) ,
68+ crate :: spec:: PrimitiveLiteral :: String ( s) => Ok ( Value :: String ( s. clone ( ) ) ) ,
69+ _ => Err ( Error :: new (
70+ ErrorKind :: FeatureUnsupported ,
71+ "Unsupported literal type to convert to json" ,
72+ ) ) ,
73+ } ,
74+ _ => Err ( Error :: new (
75+ ErrorKind :: FeatureUnsupported ,
76+ "Unsupported literal type to convert to json" ,
77+ ) ) ,
78+ }
79+ }
80+
4681struct SchemaToAvroSchema {
4782 schema : String ,
4883}
@@ -92,9 +127,12 @@ impl SchemaVisitor for SchemaToAvroSchema {
92127 custom_attributes : Default :: default ( ) ,
93128 } ;
94129
95- if !field. required {
130+ if let Some ( default) = & field. initial_default {
131+ avro_record_field. default = Some ( literal_to_json ( default) ?) ;
132+ } else if !field. required {
96133 avro_record_field. default = Some ( Value :: Null ) ;
97134 }
135+
98136 avro_record_field. custom_attributes . insert (
99137 FILED_ID_PROP . to_string ( ) ,
100138 Value :: Number ( Number :: from ( field. id ) ) ,
0 commit comments