duanpenpan5796 2018-07-06 21:16
浏览 53
已采纳

Lambda Golang PutItem和MarshalMap到DynamoDB

I'm trying to load data from Lambda using Golang into DynamoDB, however the marshalling method is just generating empty items. I have a type struct defined as below...

type Flight struct {
    id          string
    destination string
    airline     string
    time        string
    latest      string
}

I am then populating a slice as below.....

func PutToDynamo(Flights []Flight, kind string) {

Flights := []Flight{}

for _, row := range rows {
    columns := row.FindAll("td")

    f := columns[0].Text() //all these are strings
    a := columns[1].Text()
    i := columns[2].Text()
    t := columns[4].Text()
    l := columns[5].Text()

    flight := Flight{
        id:          i,
        destination: f,
        airline:     a,
        time:        t,
        latest:      l,
    }
    Flights = append(Flights, flight)

Which I then try and load into DynamoDB.

func PutToDynamo(flights []Flight, kind string) {
    for _, flight := range flights {
        av, err := dynamodbattribute.MarshalMap(flight)

        input := &dynamodb.PutItemInput{
            Item:      av,
            TableName: aws.String("flights"),
        }

        _, err = svc.PutItem(input)
        fmt.Println("FLIGHT: ", input.GoString())

If I print flight, I can see all the information I expect. However, input.GoString() is just returning the below...

 {
   Item: {
   },
   TableName: "flights"
 }

and I get an error thrown from DynamoDB in lambda.

ValidationException: One or more parameter values were invalid: Missing the key id in the item

Any ideas?? I've tried putting json:"id" etc in the struct, but no difference.

Thanks!

  • 写回答

1条回答 默认 最新

  • dotibrb048760 2018-07-06 21:53
    关注

    The fields from your structure are unexported, and this may be the reason why dynamo marshal is not marshaling it (quick look at the source code suggests that dynamo and json marshal methods are a bit similar). According to regular, golang json Marshal, from documentation:

    The json package only accesses the exported fields of struct types (those that begin with an uppercase letter). Therefore only the the exported fields of a struct will be present in the JSON output.

    There are two ways you can solve this:

    a) Change your struct to have exported fields.

    type Flight struct {
        Id          string
        Destination string
        Airline     string
        Time        string
        Latest      string
    }
    

    b) If you don't want to change your struct (for example if its referenced a lot in your code and refactoring would be a hassle), you can add a custom Marshal and Unmarhsal functions and hide your struct between a custom made export struct (code below is untested - its just to give a brief idea):

    type Flight struct {
        id          string
        destination string
        airline     string
        time        string
        latest      string
    }
    
    type FlightExport struct {
            Id          string
            Destination string
            Airline     string
            Time        string
            Latest      string
    }
    
    func(f *Flight) MarshalJSON()([] byte, error) {
        fe: = &FlightExport {
            ID: f.id,
            Destination: f.destination,
            Airline: f.airline,
            Time: f.time,
            Latest: f.latest
        }
    
            return json.Marshal(fe)
    }
    
    func(f *Flight) UnmarshalJSON(data[] byte) error {
        var fe FlightExport
        err: = json.Unmarshal(data, &fe)
        if err != nil {
            return err
        }
    
        f.id = fe.ID
        f.destination = fe.Destination
        f.airline = fe.Airline
        f.time = fe.Time
        f.latest = fe.Latest
    
        return nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题