Skip to content

Commit

Permalink
update DataTypes DynamoDB Marshaling and Unmarshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
radsec committed Mar 30, 2024
1 parent dd9d912 commit 213c74a
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 173 deletions.
3 changes: 1 addition & 2 deletions pkg/model/syncstate/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func Test_GetIntendedConfig(t *testing.T) {
}

for _, test := range cases {
dataTypeAV, _ := test.expectedDataType.MarshalDynamoDBAttributeValue()
dynamodb := getSyncState(
func(key dynamodb.PrimaryKey, consistentRead bool) (*awsdynamodb.GetItemOutput, error) {
if test.dbError {
Expand All @@ -66,7 +65,7 @@ func Test_GetIntendedConfig(t *testing.T) {
Item: map[string]awstypes.AttributeValue{
"CleanSync": &awstypes.AttributeValueMemberBOOL{Value: false},
"BatchSize": &awstypes.AttributeValueMemberN{Value: "31"},
"DataType": dataTypeAV,
"DataType": &awstypes.AttributeValueMemberS{Value: string(test.expectedDataType)},
},
}, nil
}
Expand Down
52 changes: 42 additions & 10 deletions pkg/types/data_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,32 @@ func (dt *DataType) UnmarshalText(text []byte) error {
case "SENSOR_DATA":
fallthrough
case "SENSORDATA":
fallthrough
case "SensorData":
*dt = DataTypeSensorData
case "RULES_FEED":
fallthrough
case "RULESFEED":
fallthrough
case "RulesFeed":
*dt = DataTypeRulesFeed
case "SYNC_STATE":
fallthrough
case "SYNCSTATE":
fallthrough
case "SyncState":
*dt = DataTypeSyncState
case "MACHINE_CONFIG":
fallthrough
case "MACHINECONFIG":
*dt = DataTypeGlobalConfig
fallthrough
case "MachineConfig":
*dt = DataTypeMachineConfig
case "GLOBAL_CONFIG":
fallthrough
case "GLOBALCONFIG":
fallthrough
case "GlobalConfig":
*dt = DataTypeGlobalConfig
default:
return fmt.Errorf("unknown data_type value %q", mode)
Expand All @@ -50,15 +60,15 @@ func (dt *DataType) UnmarshalText(text []byte) error {
func (dt DataType) MarshalText() ([]byte, error) {
switch dt {
case DataTypeSensorData:
return []byte("SENSORDATA"), nil
return []byte("SensorData"), nil
case DataTypeSyncState:
return []byte("SYNCSTATE"), nil
return []byte("SyncState"), nil
case DataTypeMachineConfig:
return []byte("MACHINECONFIG"), nil
return []byte("MachineConfig"), nil
case DataTypeGlobalConfig:
return []byte("GLOBALCONFIG"), nil
return []byte("GlobalConfig"), nil
case DataTypeRulesFeed:
return []byte("RULESFEED"), nil
return []byte("RulesFeed"), nil
default:
return nil, fmt.Errorf("unknown data_type %s", dt)
}
Expand All @@ -85,12 +95,10 @@ func (dt DataType) MarshalDynamoDBAttributeValue() (awstypes.AttributeValue, err
}

// UnmarshalDynamoDBAttributeValue implements the Unmarshaler interface
// func (dt *DataType) UnmarshalDynamoDBAttributeValue(av *dynamodb.AttributeValue) error {
func (dt *DataType) UnmarshalDynamoDBAttributeValue(av awstypes.AttributeValue) error {
// return attributevalue.Unmarshal(av, dt)
v, ok := av.(*awstypes.AttributeValueMemberN)
v, ok := av.(*awstypes.AttributeValueMemberS)
if !ok {
return fmt.Errorf("unexpected data_type value type %T", av)
return fmt.Errorf("unexpected data_type value type: %T", av)
}

switch t := v.Value; t {
Expand All @@ -99,17 +107,41 @@ func (dt *DataType) UnmarshalDynamoDBAttributeValue(av awstypes.AttributeValue)
case "SENSOR_DATA":
fallthrough
case "SENSORDATA":
fallthrough
case "SensorData":
*dt = DataTypeSensorData
case "2":
fallthrough
case "SYNC_STATE":
fallthrough
case "SYNCSTATE":
fallthrough
case "SyncState":
*dt = DataTypeSyncState
case "3":
fallthrough
case "GLOBAL_CONFIG":
fallthrough
case "GLOBALCONFIG":
fallthrough
case "GlobalConfig":
*dt = DataTypeGlobalConfig
case "4":
fallthrough
case "MACHINE_CONFIG":
fallthrough
case "MACHINECONFIG":
fallthrough
case "MachineConfig":
*dt = DataTypeMachineConfig
case "5":
fallthrough
case "RULES_FEED":
fallthrough
case "RULESFEED":
fallthrough
case "RulesFeed":
*dt = DataTypeRulesFeed
default:
return fmt.Errorf("unknown data_type value %q", t)
}
Expand Down
Loading

0 comments on commit 213c74a

Please sign in to comment.