|
| 1 | +package converter |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/awakari/source-websocket/model" |
| 7 | + "github.com/cloudevents/sdk-go/binding/format/protobuf/v2/pb" |
| 8 | + "github.com/segmentio/ksuid" |
| 9 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 10 | + "reflect" |
| 11 | + "strconv" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +type Service interface { |
| 16 | + Convert(src string, raw map[string]any) (evt *pb.CloudEvent, err error) |
| 17 | +} |
| 18 | + |
| 19 | +type svc struct { |
| 20 | + et string |
| 21 | +} |
| 22 | + |
| 23 | +type ConvertFunc func(evt *pb.CloudEvent, v any) (err error) |
| 24 | + |
| 25 | +const seismicportalEuEventDetailsHtmlUnid = "https://www.seismicportal.eu/eventdetails.html?unid=" |
| 26 | + |
| 27 | +var convSchema = map[string]any{ |
| 28 | + "action": toStringFunc(model.CeKeyAction), |
| 29 | + "data": map[string]any{ |
| 30 | + "properties": map[string]any{ |
| 31 | + "auth": toStringFunc(model.CeKeySubject), |
| 32 | + "depth": toStringWithPrefixFunc(model.CeKeyElevation, "-"), |
| 33 | + "flynn_region": toStringFunc(model.CeKeyLocation), |
| 34 | + "lat": toStringFunc(model.CeKeyLatitude), |
| 35 | + "lon": toStringFunc(model.CeKeyLongitude), |
| 36 | + "mag": toStringFunc(model.CeKeyMagnitude), |
| 37 | + "magtype": toStringFunc(model.CeKeyMagnitudeType), |
| 38 | + "sourcecatalog": toStringFunc(model.CeKeySourceCatalog), |
| 39 | + "sourceid": toStringFunc(model.CeKeySourceId), |
| 40 | + "time": toTimestampFunc(model.CeKeyTime), |
| 41 | + "unid": toStringWithPrefixFunc(model.CeKeyObjectUrl, seismicportalEuEventDetailsHtmlUnid), |
| 42 | + }, |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +var ErrConversion = errors.New("conversion failure") |
| 47 | + |
| 48 | +func NewService(et string) Service { |
| 49 | + return svc{ |
| 50 | + et: et, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (s svc) Convert(src string, raw map[string]any) (evt *pb.CloudEvent, err error) { |
| 55 | + evt = &pb.CloudEvent{ |
| 56 | + Id: ksuid.New().String(), |
| 57 | + Source: src, |
| 58 | + SpecVersion: model.CeSpecVersion, |
| 59 | + Type: s.et, |
| 60 | + Attributes: make(map[string]*pb.CloudEventAttributeValue), |
| 61 | + Data: &pb.CloudEvent_TextData{}, |
| 62 | + } |
| 63 | + err = convert(evt, raw, convSchema) |
| 64 | + return |
| 65 | +} |
| 66 | + |
| 67 | +func convert(evt *pb.CloudEvent, node map[string]any, schema map[string]any) (err error) { |
| 68 | + for k, v := range node { |
| 69 | + schemaChild, schemaChildOk := schema[k] |
| 70 | + if schemaChildOk { |
| 71 | + switch schemaChildT := schemaChild.(type) { |
| 72 | + case ConvertFunc: |
| 73 | + err = errors.Join(err, schemaChildT(evt, v)) |
| 74 | + case map[string]any: |
| 75 | + branch, branchOk := v.(map[string]any) |
| 76 | + if branchOk { |
| 77 | + err = errors.Join(convert(evt, branch, schemaChildT)) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return |
| 83 | +} |
| 84 | + |
| 85 | +func toStringFunc(k string) ConvertFunc { |
| 86 | + return func(evt *pb.CloudEvent, v any) (err error) { |
| 87 | + var str string |
| 88 | + str, err = toString(k, v) |
| 89 | + if err == nil { |
| 90 | + evt.Attributes[k] = &pb.CloudEventAttributeValue{ |
| 91 | + Attr: &pb.CloudEventAttributeValue_CeString{ |
| 92 | + CeString: str, |
| 93 | + }, |
| 94 | + } |
| 95 | + } |
| 96 | + return |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func toString(k string, v any) (str string, err error) { |
| 101 | + switch vt := v.(type) { |
| 102 | + case bool: |
| 103 | + str = strconv.FormatBool(vt) |
| 104 | + case int: |
| 105 | + str = strconv.Itoa(vt) |
| 106 | + case int8: |
| 107 | + str = strconv.Itoa(int(vt)) |
| 108 | + case int16: |
| 109 | + str = strconv.Itoa(int(vt)) |
| 110 | + case int32: |
| 111 | + str = strconv.Itoa(int(vt)) |
| 112 | + case int64: |
| 113 | + str = strconv.FormatInt(vt, 10) |
| 114 | + case float32, float64: |
| 115 | + str = fmt.Sprintf("%f", vt) |
| 116 | + case string: |
| 117 | + str = vt |
| 118 | + default: |
| 119 | + err = fmt.Errorf("%w: key: %s, value: %v, type: %s, expected: string/bool/int/float", ErrConversion, k, v, reflect.TypeOf(v)) |
| 120 | + } |
| 121 | + return |
| 122 | +} |
| 123 | + |
| 124 | +func toStringWithPrefixFunc(k, prefix string) ConvertFunc { |
| 125 | + return func(evt *pb.CloudEvent, v any) (err error) { |
| 126 | + var str string |
| 127 | + str, err = toString(k, v) |
| 128 | + if err == nil { |
| 129 | + evt.Attributes[k] = &pb.CloudEventAttributeValue{ |
| 130 | + Attr: &pb.CloudEventAttributeValue_CeString{ |
| 131 | + CeString: prefix + str, |
| 132 | + }, |
| 133 | + } |
| 134 | + } |
| 135 | + return |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func toTimestampFunc(k string) ConvertFunc { |
| 140 | + return func(evt *pb.CloudEvent, v any) (err error) { |
| 141 | + str, strOk := v.(string) |
| 142 | + switch strOk { |
| 143 | + case true: |
| 144 | + var t time.Time |
| 145 | + t, err = time.Parse(time.RFC3339, str) |
| 146 | + evt.Attributes[k] = &pb.CloudEventAttributeValue{ |
| 147 | + Attr: &pb.CloudEventAttributeValue_CeTimestamp{ |
| 148 | + CeTimestamp: timestamppb.New(t), |
| 149 | + }, |
| 150 | + } |
| 151 | + default: |
| 152 | + err = fmt.Errorf("%w: key: %s, value %v, type: %s, expected timestamp in RFC3339 format", ErrConversion, k, v, reflect.TypeOf(k)) |
| 153 | + } |
| 154 | + return |
| 155 | + } |
| 156 | +} |
0 commit comments