Denmaseno 9 anos atrás
pai
commit
369da28839
2 arquivos alterados com 48 adições e 9 exclusões
  1. 21 9
      gotmpl.go
  2. 27 0
      sample.xml

+ 21 - 9
gotmpl.go

@@ -15,11 +15,6 @@ import (
 	"code.senomas.com/go/util"
 )
 
-// Variables struct
-type Variables struct {
-	Env map[string]string
-}
-
 func parseYAML(text string) map[string]interface{} {
 	val := make(map[string]interface{})
 	err := yaml.Unmarshal([]byte(text), &val)
@@ -35,8 +30,15 @@ func parseJSON(text string) map[string]interface{} {
 }
 
 func value(vs ...interface{}) interface{} {
+	fmt.Printf("TEST VALUES [%v]\n", vs)
 	for _, v := range vs {
-		if v != nil {
+		if vv, ok := v.(string); ok {
+			fmt.Printf("TEST VALUE STRING [%s]\n", vv)
+			if len(vv) > 0 {
+				return vv
+			}
+		} else if v != nil {
+			fmt.Printf("TEST VALUE ELSE [%v]\n", vv)
 			return v
 		}
 	}
@@ -58,8 +60,18 @@ func split(v string, sep string) []string {
 	return strings.Split(v, sep)
 }
 
-func ift(test bool, vt, vf interface{}) interface{} {
-	if test {
+func ift(test interface{}, vt, vf interface{}) interface{} {
+	var tb bool
+	if tv, ok := test.(bool); ok {
+		tb = tv
+	} else if tv, ok := test.(string); ok {
+		tb = len(tv) > 0
+	} else if tv, ok := test.(int); ok {
+		tb = tv != 0
+	} else {
+		tb = test != nil
+	}
+	if tb {
 		return vt
 	}
 	return vf
@@ -119,7 +131,7 @@ func main() {
 	}
 
 	var bb bytes.Buffer
-	err = tmpl.Execute(&bb, &Variables{env})
+	err = tmpl.Execute(&bb, env)
 	util.Check("Execute error %v", err)
 	fmt.Println(html.UnescapeString(bb.String()))
 }

+ 27 - 0
sample.xml

@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<datasources>
+  <{{ ift ( value .XBRLGEN_DS_XA true ) "xa-datasource" "datasource" }} jta="{{ value .XBRLGEN_DS_JTA .XBRLGEN_DS_XA true }}" jndi-name="{{ value .XBRLGEN_DS_JNDI "java:/xbrl-gen" }}" pool-name="{{ value .XBRLGEN_DS_POOLNAME "xbrl-gen" }}" use-java-context="{{ value .XBRLGEN_DS_USE_JAVA_CTX true }}" use-ccm="{{ value .XBRLGEN_DS_USE_CCM true }}">
+  {{- if ( value .XBRLGEN_DS_XA true ) }}
+    <xa-datasource-property name="URL">{{ value .XBRLGEN_DS_URL "java:mysql://mysql:3306/xbrlgen" }}</xa-datasource-property>
+  {{- else }}
+    <connection-url>{{ value .XBRLGEN_DS_URL "java:mysql://mysql:3306/xbrlgen" }}</connection-url>
+  {{- end }}
+  {{ if .XBRLGEN_DS_DRIVER -}}
+    <driver>{{ .XBRLGEN_DS_DRIVER }}</driver>
+  {{ else -}}
+    <driver>{{ index ( split ( value .XBRLGEN_DS_URL "java:mysql://mysql:3306/xbrlgen" ) ":" ) 1 }}</driver>
+  {{ end -}}
+    <security>
+      <user-name>{{ value .XBRLGEN_DS_USER "root" }}</user-name>
+      <password>{{ value .XBRLGEN_DS_PASSWORD "p4ssw0rd" }}</password>
+    </security>
+    <timeout>
+      <blocking-timeout-millis>30000</blocking-timeout-millis>
+      <idle-timeout-minutes>5</idle-timeout-minutes>
+    </timeout>
+    <statement>
+      <prepared-statement-cache-size>100</prepared-statement-cache-size>
+      <share-prepared-statements>true</share-prepared-statements>
+    </statement>
+  </{{ ift ( value .XBRLGEN_DS_XA true ) "xa-datasource" "datasource" }}>
+</datasources>