GithubHelp home page GithubHelp logo

fcf's People

Contributors

zevdg avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

dfischer

fcf's Issues

Assistance with proper struct setup

I'm having a bit of trouble figuring out the right way to setup my types to pick up this structure.

On my server this is the natural type:

// Assessment stores assessment in db
type Assessment struct {
	TemplateID string               `firestore:"templateID"`
	Created    time.Time            `firestore:"created"`
	UserID     string               `firestore:"userID"`
	Slots      map[string]*bot.Slot `firestore:"slots"`
}

type Slot struct {
	Value   interface{} `firestore:"value"`
	Created time.Time   `firestore:"created"`
}

Which then gets sent as this to the firestore trigger cloud function:

{
  "oldValue": {},
  "updateMask": {},
  "value": {
    "createTime": "2019-06-13T22:44:02.081775Z",
    "fields": {
      "created": {
        "timestampValue": "2019-06-13T22:44:01.926976Z"
      },
      "slots": {
        "mapValue": {
          "fields": {
            "frustrated": {
              "mapValue": {
                "fields": {
                  "created": {
                    "timestampValue": "2019-06-13T22:43:57.332848Z"
                  },
                  "value": {
                    "stringValue": "zxc"
                  }
                }
              }
            },
            "hitQuota": {
              "mapValue": {
                "fields": {
                  "created": {
                    "timestampValue": "2019-06-13T22:43:53.571678Z"
                  },
                  "value": {
                    "stringValue": "qwe"
                  }
                }
              }
            },
            "toolsTechnology": {
              "mapValue": {
                "fields": {
                  "created": {
                    "timestampValue": "2019-06-13T22:43:55.566685Z"
                  },
                  "value": {
                    "stringValue": "asd"
                  }
                }
              }
            }
          }
        }
      },
      "templateID": {
        "stringValue": "ae"
      },
      "userID": {
        "stringValue": "1667237206656764"
      }
    },
    "name": "removed",
    "updateTime": "2019-06-13T22:44:02.081775Z"
  }
}

The goal is to iterate over every slot easily and grab that value. That's where I'm having the most trouble. I'm close but... feel like I may be doing it wrong and missing something from a mental model perspective.

Here's the type on the cloud function

type assessmentData struct {
	TemplateID string    `fcf:"templateID"`
	Created    time.Time `fcf:"created"`
	UserID     string    `fcf:"userID"`

	MapVal map[string]struct {
		ValString map[string]string `json:"stringValue"`
	} `fcf:"slots"`
	MapDynamic   map[string]interface{} `fcf:"slots"`
	MapInterface interface{}            `fcf:"slots"`
}

I'm experimenting so there's the superfluous last two attributes.

Basically just want to output each value in slot as finishing my experiment.

	for k, v := range a.MapVal {
		log.Printf("MapVal: k: %s, val: %+v", k, v)
		log.Printf("MapVal: v : %+v", v)
		for kk, vv := range v.ValString { // this would be the slot attributes
			log.Printf("ValString: kk: %s, val: %+v", kk, vv)
			log.Printf("ValString: vv: %+v", vv)
		}
	}

I tried it without having the nested struct with ValString but wasn't having much luck there so trying to do this piece more manually but still close but no cigar.

Anyway, thanks!

edit: note:

Usually I'd expect to just use a map[string]type structure for dynamic keys but this is proving difficult with the payload - want to simplify if possible.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.